Hi there
I want to convert a float to its absolute value and I have discovered the fabs() function but it only works if I set my variable to a double. I really want my variable to remain a float, is there a way to do this?
Thanks
Printable View
Hi there
I want to convert a float to its absolute value and I have discovered the fabs() function but it only works if I set my variable to a double. I really want my variable to remain a float, is there a way to do this?
Thanks
fabs() will work with floats, they'll just be promoted within the function.
If you want to keep using floats (for whatever reason) then just make sure that the variable you are returning into is a float. Your compiler may warn you about possible loss of data but you can either ignore these warnings or cast the function to a float -
float y = (float)fabs(x);
if ( x < 0.0 ) x = -x;
Thanks Zen and Salem!
Excellent help.