smallest double value greater zero

Oct 30, 2011 at 2:58pm
Hello,

a few days ago i was wondering what the minimum value greater 0.0 is, that a double can represent. i checked std::numerical_limits< double >. but i found two different "mins" there:
min() and denorm_min().

they give me different values. so i made my own minimum double, by setting only the last bit to 1 (see code below) and it turned out, that this value does not math the other two.
now my question is:
what is the difference between min() and denorm_min()
and why does not my code match any of these two?
i looked up the doc here, but i dont understand it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>

int main(int argc, char *argv[])
{   
        double nl_dbl_min = std::numeric_limits< double >::min();
        double nl_dbl_dmin = std::numeric_limits< double >::denorm_min();
        int *p = new int[2];
        *p = 0;
        *(p + 1) = 1;
        double *my_dbl_min = reinterpret_cast< double* >(p);
        std::cout << nl_dbl_min << '\t' << nl_dbl_dmin << '\t' << *my_dbl_min << std::endl;
        delete p;
        return 0;
}

with the output
2.22507e-308    4.94066e-324    2.122e-314
Last edited on Oct 30, 2011 at 3:02pm
Oct 30, 2011 at 3:38pm
You're packing your int array back to front.

000000001 00000000 -> 2.122e-314
000000000 00000001 -> 4.94066e-324

swapping your code to 1 and then 0, rather than vice versa...

1
2
3
        int *p = new int[2];
        *p = 1;
        *(p + 1) = 0;


should do the trick!

Andy

Double precision floating-point format
http://en.wikipedia.org/wiki/Double_precision

Endianness
http://en.wikipedia.org/wiki/Endianness

Last edited on Oct 30, 2011 at 3:40pm
Oct 30, 2011 at 4:01pm
thanks a lot andy.
that solves one problem.

but im still wondering what the difference is between min and denorm_min
Oct 30, 2011 at 4:19pm
I have to admit I've never thought about this -- but I think this might help.

Denormal number
http://en.wikipedia.org/wiki/Denormal_number

I think it means that the normal min is the smallest number that can be accurately represented. Below that the representation gets fuzzier and fuzzier, due to the limited number of bits available.
Last edited on Oct 30, 2011 at 4:20pm
Oct 30, 2011 at 4:26pm
Floating point representation is usually in normal form, a bit like scientific notation with one digit in front of the decimal point and an exponent

e.g. 1.2345x10^-50 and so on.

suppose the smallest exponent we could have was 10^-100 and we had 5 digit representation. The the smallest positive number is

1.0000x10^-100, the next number above this is 1.0001x10^-100 but the next smallest one would be 0.

so there is a small jump to the next biggest but a larger jump downwards to zero, a sort of hole in representation around zero.

denormal number are used to fill this hole, this is done by moving away from the normal representation. so the next number down
would become

0.9999x10^-100 and there would be more denormal numbers down to 0.0001x10^-100
Topic archived. No new replies allowed.