Reputation: 141
If I divide a float 0 by a negative value I get displayed -0 which is inconvenient. How can I get rid of this issue?
float a = 0;
std::cout << a / (-1);
I've tried to check if a number is equal with -0 and if that condition were true, to multiply the variable by -1. This doesn't work because 0 is equal to -0.
Upvotes: 4
Views: 122
Reputation: 234875
Writing a + 0.0f
gets rid of the signed zero for a float
type a
.
(This is defined by IEEE754 and is a standard trick well-known to numericists.)
So if you have an expression that might yield a signed zero, append 0.0f
to the end:
std::cout << a / (-1) + 0.0f;
Upvotes: 5