Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check for minus 0 in cpp? [duplicate]

Tags:

c++

This might be a silly question, but I really have an issue here with 0 and minus 0.

I am calculating a slope of a function and depending on the vector ( right or left ) it is 0 or -0.

Is it possible to do a if condition and find out if the slope is 0 or -0 ? Doing the below doesnt work.

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

I deliberatly didnt put working source code, because I think it is irrelevant for the question. A simple answer for the above will help.

like image 356
infoclogged Avatar asked Dec 24 '22 10:12

infoclogged


1 Answers

You're looking for std::signbit. Both zeroes are equal to each other so your check doesn't work, but the signbit is different.

like image 99
MSalters Avatar answered Dec 25 '22 23:12

MSalters