I have run into a situation in my code where a function returns a double, and it is possible for this double to be a zero, a negative zero, or another value entirely. I need to distinguish between zero and negative zero, but the default double comparison does not. Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede. How can I distinguish between the two?
-
try -0.0 instead of -0 – sliser Jan 06 '14 at 09:19
-
Why do you need to disambiguate these? – doctorlove Jan 06 '14 at 09:19
-
Hm, not sure if I get the question correctly. You need to compare 2 `double`s? And make difference between the positive/negative value or want to treat them just as `zero`? – Kiril Kirov Jan 06 '14 at 09:19
-
Comparing floating point values for equality is almost always wrong. Why do you need it? – n. m. could be an AI Jan 06 '14 at 09:23
-
I assume that a negative 0 is the number with all bits 0, except the sin bit which is 1. Can you give an example of how you can actually get that number? no arithmetic operations I can think of can lead to this. – Pandrei Jan 06 '14 at 09:23
-
3@Pandrei: For example, `1e-300 / -1e300`. – Oliver Charlesworth Jan 06 '14 at 09:24
-
7@n.m. comparing for exact equality is often correct. And negative zero was designed for a specific use case. In order to make use of neg zero, you have to detect it. – David Heffernan Jan 06 '14 at 09:26
-
2@BenjaminBannier Not a duplicate of that. That other question, as it turned out, wasn't even about negative zero in the first place, it was just a normal negative number that was small enough that with default precision, it looked like negative zero when printed. – Jan 06 '14 at 09:49
-
@benjamin This is not a dupe of that Q – David Heffernan Jan 06 '14 at 09:54
-
Ups, yes, you are both right. – Benjamin Bannier Jan 06 '14 at 10:27
3 Answers
Call std::signbit()
to determine the state of the sign bit.

- 601,492
- 42
- 1,072
- 1,490
Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede.
First off, C++ doesn't mandate any standard for float
/double
types.
Assuming you're talking about IEEE 754's binary32 and binary64 formats, they're specifically designed to maintain their order when their bit patterns are interpreted as integers so that a non-FPU can sort them; this is the reason they have a biased exponent.
There're many SO posts discussing such comparisons; here's the most relevant one. A simple check would be
bool is_negative_zero(float val)
{
return ((val == 0.0f) && std::signbit(val));
}
This works since 0.0f == -0.0f
, although there're places where the sign makes a difference like atan2
or when dividing by -0 as opposed to +0 leads to the respective infinities.
To test explicitly for a == -0 in C do the following:
if (*((long *)&a) == 0x8000000000000000) {
// a is -0
}

- 29
- 3
-
2... if (_and only if_) you know the platform uses IEC 60559 representation in that endianness, and if `long` is the same size as `double` ... – Toby Speight Aug 08 '19 at 17:49