infinite numbers and ...

hi i was working on float and double data types and to see the results i wrote this program:

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    ofstream outputF("output.txt");
    double a = 1;
    double outcome;


    outputF << setprecision(19);
    outputF << "//---------------------" << endl;
    for(double b = 1;b <= 100;b++)
    {
        outcome = a / b;
        outputF << "for the value of : " << setfill('0') 
                << setw(3) << b << endl << endl;
        outputF << outcome << endl 
                << "//---------------------" << endl;
    }

    return 0;
}


well i understand the part it cannot store infinite numbers. but if you take a look at the output for example (since it is too long i just added some of the outputs)

//---------------------
for the value of : 001

1
//---------------------
for the value of : 002

0.5
//---------------------
for the value of : 003

0.3333333333333333148
//---------------------
for the value of : 004

0.25
//---------------------
for the value of : 005

0.2000000000000000111
//---------------------
for the value of : 006

0.1666666666666666574
//---------------------
for the value of : 007

0.1428571428571428492
//---------------------
for the value of : 008

0.125
//---------------------
for the value of : 009

0.1111111111111111049
//---------------------
for the value of : 010

0.1000000000000000056
//---------------------
for the value of : 011

0.09090909090909091161
//---------------------


if you look carefully at the value "5" and "10" results. it is awkwardly anormal. which is something i couldnt understand. also it is the same with value "20", "25", "40", "50" and so on.

i would google it first but i dont even know the keyword to search. why it happens this way?
1/5, in binary, is a repeating decimal and so it cannot be represented exactly in a finite amount of space. The only fractions that can be represented exactly are those where the denominator is a power of 2, hence why 2, 4, and 8 in your example look good.
hmm. interesting so actually the opacity and volumes i see in programs %100 isnt 100, actually they are 256 right?

whatever ,your answer was way enough. ty
@Ceset
You could read books about Principles of Computer Orgnization, there is a chapter on float number representing, it's kinda boring but it will sweep all your questions clearly about float number.
well. i m not very good with books. especially when they are about computers. it feels so boring. i can read math or physic books without getting bored though. but not about computer
The numbers printed out would appear to be more reasonable with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <limits>

int main()
{
    const double a = 1 ;

    constexpr int max_precision = std::numeric_limits<double>::digits10 ;
    std::cout << std::fixed << std::setprecision(max_precision) ;

    std::cout << "//---------------------\n" ;
    for( int b = 1 ; b < 30 ; ++b )
    {
        const double outcome = a / b ;
        std::cout << "for b == " << std::setw(3) << b
                   << " result: " << outcome << '\n' ;
    }
}

http://ideone.com/Mh6i96
Topic archived. No new replies allowed.