Is it possible to check minus 0 in cpp?

It might be a dumb question, but I really have a problem with 0 and minus 0.

I calculate the slope of the function and depending on the vector (right or left) is 0 or -0.

Is it possible to fulfill the if condition and find out if the angle of inclination is 0 or -0? The execution below does not work.

if ( slope == 0.0f )
std::cout << "direction vector towards right";
else if ( slope == -0.0f )
std::cout << "direction vector towards left"

I intentionally did not use working source code, because I think it does not matter for the question. A simple answer to this will help.

+4
source share
1 answer

You are looking for std::signbit. Both zeros are equal to each other, so your check does not work, but the sign is different.

+10
source

Source: https://habr.com/ru/post/1693166/


All Articles