Feuerteufel
Feuerteufel

Reputation: 581

How is float_max + 1 defined in C++?

What is that actual value of f?

float f = std::numeric_limits<float>::max() + 1.0f;

For unsigned integral types it is well defined to overflow to 0, and for signed integral it is undefined/implementation specific if I'm not wrong. But how is it specified in standard for float/double? Is it std::numeric_limits<float>::max() or does it become std::numeric_limits<float>::infinity()?

On cppreference I didn't find a specification so far, maybe I missed it.

Thanks for help!

Upvotes: 5

Views: 830

Answers (1)

alias
alias

Reputation: 30428

In any rounding mode, max + 1 will simply be max with an IEEE-754 single-precision float.

Note that the maximum positive finite 32-bit float is:

                  3  2          1         0
                  1 09876543 21098765432109876543210
                  S ---E8--- ----------F23----------
          Binary: 0 11111110 11111111111111111111111
             Hex: 7F7F FFFF
       Precision: SP
            Sign: Positive
        Exponent: 127 (Stored: 254, Bias: 127)
       Hex-float: +0x1.fffffep127
           Value: +3.4028235e38 (NORMAL)

For this number to overflow and become infinity using the default rounding mode of round-nearest-ties-to-even, you have to add at least:

                  3  2          1         0
                  1 09876543 21098765432109876543210
                  S ---E8--- ----------F23----------
          Binary: 0 11100110 00000000000000000000000
             Hex: 7300 0000
       Precision: SP
            Sign: Positive
        Exponent: 103 (Stored: 230, Bias: 127)
       Hex-float: +0x1p103
           Value: +1.0141205e31 (NORMAL)

Anything you add less than this particular value will round it back to max value itself. Different rounding modes might have slightly different results, but the order of the number you're looking for is about 1e31, which is pretty darn large.

This is an excellent example of how IEEE floats get sparser and sparser as their magnitude increases.

Upvotes: 2

Related Questions