Steve
Steve

Reputation: 4097

Numeric Limits of Float Lowest and Max

In what real case does c++ std::numeric_limits<float>::lowest() not equal the negative of std::numeric_limits<float>::max()?

Upvotes: 2

Views: 1714

Answers (1)

David Scarlett
David Scarlett

Reputation: 3341

std::numeric_limits<float>::lowest() will equal the negative of std::numeric_limits<float>::max() if the floating point representation uses a signed magnitude, as the max and lowest values will have identical significand (ignoring the sign bit) and exponent, with both containing their maximum values. The sign bit will then determine whether the number is positive or negative, and will not affect its magnitude. (Another concequence of this is that the underlying representation will be able to represent both positive and negative zero.) IEEE 754 uses signed magnitude representation, so this is what you are most likely to encounter in practice.

However, an alternative representation is to use a two's complement significand. A property of two's complement is that the maximal negative value has a greater magnitude than the maximal positive value, and so for a floating point representation using a two's complement significand, std::numeric_limits<float>::lowest() will not equal the negative of std::numeric_limits<float>::max(). An example of a real system using a two's complement significand floating point format is the IBM 1130, circa 1970.

Now, cppreference.com does state:

While it's not true for fundamental C++ floating-poing types, a third-party floating-point type T may exist such that std::numeric_limits<T>::lowest() != -std::numeric_limits<T>::max().

But I'm not sure what the basis of that statement is, as the C++11 standard, paragraph 18.3.2.4, merely specifies that lowest() is:

A finite value x such that there is no other finite value y where y < x.

Meaningful for all specializations in which is_bounded != false.

It also notes in a footnote:

lowest() is necessary because not all floating-point representations have a smallest (most negative) value that is the negative of the largest (most positive) finite value.

So it would seem that a two's complement floating point representation could be used by a C++ implementation, and that would have std::numeric_limits<float>::lowest() not equal to the negative of std::numeric_limits<float>::max().

Upvotes: 3

Related Questions