42

Why does numeric_limits::min return a negative value for int, but positive values for e.g. float and double?

#include<iostream>
#include<limits>

using namespace std;

int main() {
  cout << "int: " << numeric_limits<int>::min() << " "
       << "float: " << numeric_limits<float>::min() << " "
       << "double: " << numeric_limits<double>::min() << "\n";
  return 0;
}

Output:

int: -2147483648 float: 1.17549e-38 double: 2.22507e-308

From cppreference:

Returns the minimum finite value representable by the numeric type T.

For floating-point types with denormalization, min returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min for integral types. To find the value that has no values less than it, use numeric_limits::lowest.

min is only meaningful for bounded types and for unbounded unsigned types, that is, types that represent an infinite set of negative values have no meaningful minimum.

gnzlbg
  • 7,135
  • 5
  • 53
  • 106

2 Answers2

50

By definition, for floating types, min returns the smallest positive value the type can encode, not the lowest.

If you want the lowest value, use numeric_limits::lowest instead.

Documentation: http://en.cppreference.com/w/cpp/types/numeric_limits/min

As for why it is this way, I can only speculate that the Standard committee needed to have a way to represent all forms of extreme values for all different native types. In the case of integral types, there's only two types of extreme: max positive and max negative. For floats there is another: smallest possible.

If you think the semantics are a bit muddled, I agree. The semantics of the related #defines in the C standard are muddled in much the same way.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 1
    Thanks for the speculation. It make sense that one need information about both smallest possible types. I just found it really weird that both are also defined for integers although they return the same value. Thought that maybe there was a deeper reason for that. – gnzlbg Jun 12 '13 at 16:27
  • 5
    @gnzlbg: I don't know for certian, but I doubt there is any reason deeper than simply "That's what C did." – John Dibling Jun 12 '13 at 16:28
  • are you sure about that "smallest positive value"? The output from the OP looks negative for int, and cppreference.com says "smallest finite value". My compiler is (still) not C++11 compliant, so I can't use lowest :-( – craq Feb 12 '15 at 15:01
  • 2
    What C `#define` uses the term "lowest"? I don't see it in limits.h nor float.h – JDiMatteo Apr 29 '15 at 23:32
  • 1
    @JDiMatteo: `FLT_MIN` and `DBL_MIN` are both defined in this (crazy) way in C. – j_random_hacker Dec 19 '16 at 17:21
6

It's unfortunate, but behind similar names completely different meaning lies. It was kinda carried over from C, where DBL_MIN and INT_MIN has the very same "problem".

As not much can be done, just remember what means what.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37