Search code examples
cfloating-pointc11math.hc17

If the next representable value after -0.0 is +0.0, then why nextafter(-0.0, INFINITY) does not return +0.0?


If the next representable value after -0.0 is +0.0, then why nextafter(-0.0, INFINITY) does not return +0.0?


Solution

  • 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.