Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::numeric_limits<double>::min()

35 views
Skip to first unread message

CTG

unread,
Jan 25, 2017, 7:56:48 PM1/25/17
to
bool b1,b2;

double value =0.0

if( value > std::numeric_limits<double>::min() ) b1 = 1; else b1=0;
if( value < std::numeric_limits<double>::max() ) b2 = 1; else b2=0;
cout << b1 <<","<< b2<< endl;


outputs : 0,1

What am i missing here?

Alf P. Steinbach

unread,
Jan 25, 2017, 8:05:41 PM1/25/17
to
For integers min and max are the ends of the entire value range.

For floating point types they are the ends of the strictly positive
value range. Then down from min towards 0, depending on the
implementation there may be a range of steadily less precise denormal
values. Then 0, and symmetrically down in the negative range.

That may be what you're missing, or maybe you're missing out on the
`condition? value1 : value2` notation?


Cheers!,

- Alf

CTG

unread,
Jan 25, 2017, 8:14:10 PM1/25/17
to
On Thursday, January 26, 2017 at 11:56:48 AM UTC+11, CTG wrote:
> bool b1,b2;
>
> double value =0.0
>
> if( value > std::numeric_limits<double>::min() ) b1 = 1; else b1=0; // smalleest number ( biggest negative allowed for double

Daniel

unread,
Jan 25, 2017, 8:18:00 PM1/25/17
to
Notwithstanding its name, and unlike its counterpart
std::numeric_limits<int>::min(), std::numeric_limits<double>::min() doesn't
give the minimum value for that type, it gives 0.
-std::numeric_limits<double>::max() gives the minimum double value. Why? Who
knows.

Daniel

Daniel

unread,
Jan 25, 2017, 8:30:11 PM1/25/17
to
On Wednesday, January 25, 2017 at 8:18:00 PM UTC-5, Daniel wrote:
>
> Notwithstanding its name, and unlike its counterpart
> std::numeric_limits<int>::min(), std::numeric_limits<double>::min() doesn't
> give the minimum value for that type, it gives 0.

Sorry, it gives the smallest minimum normalized positive value. So not sure about your result.

Daniel

Christiano

unread,
Jan 25, 2017, 10:55:11 PM1/25/17
to
From the standard C++11, 18.3.2.4
"For floating types with denormalization, returns the minimum positive normalized value."

step by step
For floating types: http://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html
with denormalization: http://stackoverflow.com/questions/8341395/what-is-a-subnormal-floating-point-number
returns the minimum positive: self-explanatory
normalized value: not subnormal

Calculating:
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
The minimum normal number double precision is
0 00000000001 0000000000000000000000000000000000000000000000000000
=
2^(1-1023)
=
https://www.wolframalpha.com/input/?i=%282^%28-1022%29%29
=
2.22507385850720138309023271733240406421921598046233 × 10^-308

Comparing using c++:

#include <limits>
#include <iostream>
using namespace std;

int main()
{
cout << std::numeric_limits<double>::min() << endl;
}

Result:
2.22507e-308

Chris Vine

unread,
Jan 26, 2017, 5:27:50 AM1/26/17
to
This is a weirdity inherited by C++ from C.

If you want the largest negative number supported by double rather
than the smallest positive number, use
std::numeric_limits<double>::lowest(), available since C++11. With
C++-98/03 you had to use -std::numeric_limits<double>::max() to obtain
that value, which would work with IEEE 754 floating points (ie, those
supplied by the C++ standard) but not necessarily for others.

With integer types, std::numeric_limits::min() gives the same value as
std::numeric_limits::lowest().

Daniel

unread,
Jan 26, 2017, 1:54:42 PM1/26/17
to
On Thursday, January 26, 2017 at 5:27:50 AM UTC-5, Chris Vine wrote:
>
> If you want the largest negative number supported by double rather
> than the smallest positive number, use
> std::numeric_limits<double>::lowest(), available since C++11.

One more thing I didn't know about. Thanks!

Daniel
0 new messages