Curiously, why does my abs-function return -0?
c, objective-c
Solution
-0 is an artifact of binary representation, 0 with the sign bit set. Wikipedia has a comprehensive article on signed zero if you would like further details.
Use fabs() as people have said above. If you really really really want to inline, chain your compares:
inline float abs(float a) { return (a > 0.f) ? a : ( (a < 0.f) ? -a : 0); }
Problem
The following abs-function sometimes returns `-0` (minus zero) ``` inline float abs(float a){ return( a>=0.0f? a :-a); } ``` To be more specific, the statement `sprintf(str, "%.2f", abs(-0.00f) );` produces "-0.00", and that is annoying since the string is displayed to the user. Question: 1) Why does it produce `-0`? 2) How to fix it? PS: I am using xcode's (objective) c compiler.