Display integer

With the loop below, is there a way to display the actual number without the leading zeros (scientific notation) or will it just display 0 since there are so many leading zeros?

1
2
3
4
5
6
7
8
9
10

num = 1;

while (num > 0)
{
    num /= 2;
}

cout << num;
You loop until num becomes zero so that is the value that num will have after the loop.
Perhaps we could help better if we understood what you are trying to do with this code.
Well the book I am studying from asks to write a short program to determine the smallest number, x_min, used on the computer you are using. Note that your computer will be unable to reliably distinguish between zero and a quantity that is smaller than this number.

Maybe I am going about it the wrong way
1
2
3
4
5
6
7
8
9
10
#include <cfloat>
#include <iostream>

int main() {
    std::cout << "Minimum float value: " << FLT_MIN << "\n";
    std::cout << "Minimum double value: " << DBL_MIN << "\n";
    std::cout << "Minimum long double value: " << LDBL_MIN << "\n";

    return 0;
}


Is that what you wanted? If you wanted the integral types, you can use climits as well.
> determine the smallest number, x_min, used on the computer you are using.
> Note that your computer will be unable to reliably distinguish between zero
> and a quantity that is smaller than this number.

Determining an approximate answer computationally is not too difficult.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cfloat>
#include <iomanip>

int main()
{
    std::cout << std::scientific << std::setprecision(20) 
              << "actual epsilon: " << DBL_EPSILON << '\n' ;

    double min_last = 10 ;

    for( double d = 1 ; d > 0.0001 ; d -= 0.0000001 )
    {
        double v = d ;

        double last_seen ;
        while( v > DBL_EPSILON )
        {
            last_seen = v ;
            v /= 2 ;
        }

        if( last_seen < min_last ) min_last = last_seen ;
    }

    std::cout << "  minimum seen: " << min_last << '\n' ;
}

http://coliru.stacked-crooked.com/a/5c08f980ac093d06
Note that your computer will be unable to reliably distinguish between zero and a quantity that is smaller than this number.

So, if I understand you correctly, are you looking for the smallest representable number greater than zero?

If so, you can read this:
http://stackoverflow.com/a/13699489

Also, this code gives me a pretty small value:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    double val = 1.0;
    while (val > 0)
    {
        val /= 2;
        std::cout << val << '\n';
    }
}
0.5
0.25
0.125
...
1.97626e-323
9.88131e-324
4.94066e-324
0

Look at the second-to-last line of the output.
(I can't seem to figure out a way to reliably give me just that value)
Topic archived. No new replies allowed.