Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a zero is positive or negative?

Is it possible to check if a float is a positive zero (0.0) or a negative zero (-0.0)?

I've converted the float to a String and checked if the first char is a '-', but are there any other ways?

like image 538
e4lime Avatar asked Mar 14 '14 15:03

e4lime


People also ask

How do you check if a number is negative or positive?

If a number is greater than zero, it is a positive number. If a number is less than zero, it is a negative number. If a number equals to zero, it is zero.

Is zero can be negative or positive?

The number zero is neither positive nor negative. Positive and negative numbers are sometimes called signed numbers.

Is 0 positive or not?

Signed numbers Because zero is neither positive nor negative, the term nonnegative is sometimes used to refer to a number that is either positive or zero, while nonpositive is used to refer to a number that is either negative or zero. Zero is a neutral number.

Is zero 0 positive or negative Why?

Actually, zero is neither a negative or a positive number. The whole idea of positive and negative is defined in terms of zero. Negative numbers are numbers that are smaller than zero, and positive numbers are numbers that are bigger than zero.


1 Answers

Yes, divide by it. 1 / +0.0f is +Infinity, but 1 / -0.0f is -Infinity. It's easy to find out which one it is with a simple comparison, so you get:

if (1 / x > 0)     // +0 here else     // -0 here 

(this assumes that x can only be one of the two zeroes)

like image 122
harold Avatar answered Oct 26 '22 01:10

harold