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
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;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thanks Zen and Salem!
Excellent help.