If the next representable value after -0.0
is +0.0
, then why nextafter(-0.0, INFINITY)
does not return +0.0
?
Asked
Active
Viewed 140 times
3

pmor
- 5,392
- 4
- 17
- 36
-
What does it return? – Adrian Mole Jun 28 '21 at 09:47
-
1Why should it? I don't see the logic. – klutt Jun 28 '21 at 09:54
-
3Essentially, I would suggest that `-0.0` and `+0.0` represent the *same* value. From cppreference: *IEC 60559 recommends that from is returned whenever from==to. These functions return to instead, which makes the behavior around zero consistent: std::nextafter(-0.0, +0.0) returns +0.0 and std::nextafter(+0.0, -0.0) returns -0.0.* – Adrian Mole Jun 28 '21 at 09:54
-
@AdrianMole You should write that as an answer – klutt Jun 28 '21 at 10:10
-
@klutt Meh. Maybe Sneftel could use it to edit their answer, which is essentially the same point. – Adrian Mole Jun 28 '21 at 10:14
2 Answers
4
If the next representable value after -0.0 is +0.0 ....
False premise.
-0.0
and +0.0
have the same value.
if (-0.0 == +0.0)
is true.
nextafter()
"functions determine the next representable value" (C17dr § 7.12.11.3 2)
nextafter(-0.0, INFINITY)
and nextafter(+0.0, INFINITY)
both return DBL_TRUE_MIN
.
+0.0
and -0.0
often make no functional difference. Sometimes they do make a difference.
Advanced: Be prepared for C2X with expected decimal floating point support as there are many encoding sets of decimal64 that have the same value. Repeated use of nextafter()
and friends does not form a sequence through all FP encodings.

chux - Reinstate Monica
- 143,097
- 13
- 135
- 256
-
1Ok, so for `-0.0` the _next representable value_ is not `+0.0`, because `+0.0` is the _same_ value, and not the _next_ value. – pmor Jun 28 '21 at 15:08
-
It is all about terminology. If `+0.0` is the same _value_ as `-0.0`, then will `+0.0` be the next _number_ after `-0.0`? I.e. in what terminology `+0.0` is different from `-0.0`? – pmor Jun 29 '21 at 07:55
-
@pmor Hmmm, sounds like you want some _sequence_ function. Phrases like [_total-ordering predicate_](https://en.wikipedia.org/wiki/IEEE_754#Total-ordering_predicate) are more like `nextafter()`. Perhaps [@Peter Cordes](https://stackoverflow.com/a/59349481/2410359) may help. – chux - Reinstate Monica Jun 29 '21 at 12:52
-
FYI: Just faced the case `a - b = c => b = a - c`, which fails to hold if `a` is `0.0` and `b` is `-0.0`. We have `0.0 - (-0.0) = 0.0 => b = 0.0 - 0.0 = 0.0`. The sign is lost. The `-0.0` is not recovered. – pmor Jul 11 '21 at 15:11