Advertisement

Are there any Float functions for cmath instead of Double

Started by May 04, 2003 09:39 PM
6 comments, last by jmoses 21 years, 9 months ago
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
- John Moses
Oh and for cos and sin I know I cooooould use a cos and sin table but that doesn''t work good for decimal numbers, and I need them...
- John Moses
Advertisement
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]
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
sqrtf()
sinf()
cosf()
blahf()


MPU? Is that supposed to be a Memory Processing Unit?


The x86 FPU (when not running in SIMD mode) uses 80-bit registers.
Interests: my money-pit car, computer hardware/programming, anything 3D'93 RX-7
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...
- John Moses
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.
Advertisement
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
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.

This topic is closed to new replies.

Advertisement