This is a well known issue with printf
. Since you are using a floating point number, there is no one representation possible in binary. And most of the times the binary representation is not perfect. So, 0 is stored as 0.00000000000000...0042
sometimes and as -0.000000000000000000000123
. When printf prints the second one, you get the odd -0.
I don't really know how you are comparing against 0, just remember to add an epsilon when doing floating point comparisons to adjust for oddities. E.g: to test if two floats are equal never write a == b
but fabs(a-b) < 1e-13
where 1e-13 is the epsilon (choose an exponent that suits you).