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