Reputation: 13575
I have two floating point number a
and b
. I want to check if they have different signs. The easy way is to see
bool b = a * b < 0;
But the two numbers are very small and a * b might be underflow. Any other simple way to check it?
Anyone thinking it is a duplicate question please give me an answer that exactly matches the condition a * b < 0
. Note here the sign of 0 is undefined in my question.
Upvotes: 14
Views: 4531
Reputation: 42899
You could use std::signbit as follows:
bool c = std::signbit(a) == std::signbit(b);
Another way is to use std::copysign as follows:
bool c = std::copysign(a,b) == a;
Upvotes: 10