strange rounding problems...

hello i have this simple code but it doesn't seem to work. i input 523.21563 but in the end it looks like the program has 523.21563720703125 stored which is obiously not correct could someone tell me how this comes .

my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	float nmbr=523.21563;
	float temp=nmbr-floor(nmbr);
	int count=0;
	while ((temp*10-floor(temp*10))!=0) {
		cout<<temp<<endl;
		temp=(temp*10-floor(temp*10));
		count++;
	}
	cout<<count<<endl;

	system("pause");
	return 0;
}
Last edited on
try using double instead of float. If you still have problems, then it's a limitation of computer science in general as a 32bit/64bit number will have finite precision.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
float
0.215637
0.156372
0.563721
0.637207
0.37207
0.720703
0.207031
0.0703125
0.703125
0.03125
0.3125
0.125
0.25
13
double
0.21563
0.1563
0.563
0.63
0.3
3.28509e-09
3.28509e-08
3.28509e-07
3.28509e-06
3.28509e-05
0.000328509
0.00328509
0.0328509
0.328509
0.285095
0.850949
0.509486
0.0948628
0.948628
0.48628
0.862804
0.628038
0.280382
0.803817
0.038168
0.38168
0.816795
0.167953
0.679535
0.795349
0.953491
0.534912
0.349121
0.491211
0.912109
0.121094
0.210938
0.109375
0.09375
0.9375
0.375
0.75
42


So it looks like the float stores the number as:
0.21563720703125
The double stores it as:
0.215630003285094862803816795349

However, note that all of the extra decimal places are not an indication of precision. Since data is stored as a binary number, and binary numbers will not round nicely into bases of 10, the extra digits represent the minimum that can be represented by the datatype.
Last edited on
soo how do i solve that problem ?
What's the problem? The fact that you don't have infinite precision? That's normal.

For more precision, use a double (64 bits). For EVEN more precision, use a big-number library (though that's usually only an academic thing).
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

i just want to know how many digits behind the coma my number has. and if it doesn't has the right value i can not solve it this way :/
Ah ok! That makes sense.

Check this out:
http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <limits>
using namespace std;
int main()
{
    float nmbr=523.21563;

    cout << "nmbr was entered as: " << nmbr << endl;
    cout << "It has a precision to: " << std::numeric_limits<float>::epsilon() * nmbr << endl;
}
nmbr was entered as: 523.216
It has a precision to: 6.23722e-05

So really, you can't trust it beyond 1e-4.

For a double:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <limits>
using namespace std;
int main()
{
    double nmbr=523.21563;

    cout << "nmbr was entered as: " << nmbr << endl;
    cout << "It has a precision to: " << std::numeric_limits<double>::epsilon() * nmbr << endl;
}
nmbr was entered as: 523.216
It has a precision to: 1.16177e-13

So you can trust it pretty far.
Last edited on
Topic archived. No new replies allowed.