Archived

This topic is now archived and is closed to further replies.

jmoses

Are there any Float functions for cmath instead of Double

Recommended Posts

I use sqrt(), sin() and cos() from cmath and they all return doubles, and i always typecast them floats for my game, and I was wondering if there are any sqrt, sin and cos functions that are just designed to use floats which should be twice as fast i supposed because its 4 bytes instead of 8 thanks

Share this post


Link to post
Share on other sites
There is no reason why this should be faster if a FPU is taking in doubles 8 bytes at a time. It all depends on how many bytes it is taking in. I think I read somewhere that all floats are taken to be doubles eventually, although I may be wrong.

Brendan
EDIT:
Nevermind, I forgot about all of the function+f functions for floats. But does it still make a difference?


[edited by - Punty50 on May 5, 2003 5:58:34 PM]

Share this post


Link to post
Share on other sites
Awsome thanks, I didn''t know it was as easy as sinf() and cosf()

Now does anyone know how to switch them to use degrees instead of radians? I know I can multiply them by PIOVER180 but that gets anoying... I thought they might have something built in...

Share this post


Link to post
Share on other sites
Nope, the internal processing (on the x86 FPU at least) is done by radians, so the conversion has to happen SOMEWHERE. Write a wrapper function, if you want. Or just store your angles as radians (radians are better, nyah nyah).


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I used these to calculate sin, cos and sqrt function in my 3d engine :

__forceinline const float __fastcall OXSin (const float x)
{
float r;
__asm fld DWORD PTR x
__asm fsin
__asm fstp DWORD PTR r
return r;
}

__forceinline const float __fastcall OXCos (const float x)
{
float r;
__asm fld DWORD PTR x
__asm fcos
__asm fstp DWORD PTR r
return r;
}

__forceinline void __fastcall OXSinCos (const float x, float &rSin, float &rCos)
{
float temp1, temp2;
__asm fld DWORD PTR x
__asm fsincos
__asm fstp DWORD PTR temp1
__asm fstp DWORD PTR temp2
rSin = temp1;
rCos = temp2;
}

__forceinline const float __fastcall OXSqr (const float x)
{
float r;
__asm fld DWORD PTR x
__asm fsqrt
__asm fstp DWORD PTR r
return r;
}

I hope it is of any help

Share this post


Link to post
Share on other sites
Oh why don''t you just go the whole way and use an arbitrary precision Taylor Series?

Potentially the float versions could be faster if they are optimized to not worry about the extra precision invoved in calculating the full double result... since sin/cos/tan are not single-cycle operations (last I checked...)

Note though that sinf,cosf,tanf make your code extremely unportable... I think VC++ is the only compiler that supports them... if you want to port you''ll have to do something like:
#define sinf(x) ((float)sin(x))
Not necessarily a biggie, but might be useful to know.

Share this post


Link to post
Share on other sites