Reputation: 303
I tried the following code
#include <iostream>
#include <limits>
using namespace std;
cout << numeric_limits<int>::min() << endl;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<double>::min() << endl;
cout << numeric_limits<double>::max() << endl;
which outputs
-2147483648
2147483647
2.22507e-308
1.79769e+308
Why is the minimal-double not (-1) times the maximal-double? Afaik for integers, the first bit is used as a sign (that's probably why the absolute value of max integer is lower than the absolute value of min integer, isn't it?). How are negative doubles described because for me it seems as if none of the 8 double-bytes is responsible for the sign. Till now, I read floating point numbers are symmetric. I don't quite understand this since integer values should be symmetric, too?!
Could anybody explain that for an amateur?
Upvotes: 2
Views: 2298
Reputation: 36597
For integral types, contrary to your belief, there is no requirement that the first bit represent the sign. And there are real-world representations in which that is definitely untrue.
As to the reason that an integral type may have minimum and maximum of different magnitude (i.e. one not the negative of the other) .... a typical implementation choice with current compilers is to represent integral types with some specific number of bits. The number of possible distinct values (distinct possible combinations of bit values) is 2
raised to the number of bits. So an 8
bit signed char
can represent 256
(2
to the 8
) distinct values, a 16
bit int can represent 65536
distinct values, a 32
bit int can represent 14294967296
distinct values. Note that all of those values are even. One of the representable values will be zero, which leaves an odd number of other distinct values. Which either means that values have to be doubled up (two distinct bit patterns represent the same value) or [the usual implementation choice in practice] that the maximum and minimum are not equal in magnitude. This is also, incidentally, the reason that std::numeric_limits::max()
typically gives an odd value for unsigned integral types.
As for floating point, the specification of min()
is smallest positive value (the positive value with smallest magnitude) and max()
is the largest. If you want the negative value furthest from zero, use (since C++11) std::numeric_limits<float>::lowest()
.
Upvotes: 3
Reputation: 4214
Yet another example of confusing naming in std. From http://en.cppreference.com/w/cpp/types/numeric_limits/min:
For floating-point types with denormalization, min returns the minimum positive normalized value.
Upvotes: 1
Reputation: 21514
numeric_limits behaves like that:
See http://en.cppreference.com/w/cpp/types/numeric_limits/min:
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.
cout << numeric_limits<double>::lowest() << endl;
Will show what you expected (1.79769e+308
)
Upvotes: 2