Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Store double in int variable error

This is a small program that calculates the gaps of 'interval' size that are beetween two numbers 'from' 'to'. Then I calculate the 'size' (number of gaps) and store it in an int variable, and give me a smaller value sometimes.

Here is the code:

double from=0, to=1, interval=0.1;

cout << "WORKING WITH VARIABLES: " << endl;
double operation = (to-from)/interval +1;
cout << "Size: " << operation << endl;

int size = operation;

cout << "Size after storing: " << size << endl << endl;

cout << "WORKING WITHOUT VARIABLES: " << endl;
cout << "Size: " << (to-from)/interval +1 << endl;

size = (to-from)/interval +1;

cout << "Size after storing: " << size << endl << endl;

Problem seems to be in how it's stored interval. If interval=1 everything is good, but if is 0.1, as in the example it give me 10 instead 11 in the "Size after storing" of the second case.

I've found out that it works well with interval=0.25 (2^-2).

EDIT: I haven't found that it fails in the first case, always does in the second.

like image 295
Jumer Avatar asked Jan 11 '23 04:01

Jumer


1 Answers

Floating point numbers are stored with a finite precision, and in binary. 0.25 is easy. That's just 1/4, so 0.01 binary. 0.1 is 1/10, which cannot be represented by a finite binary string. It's 1/16+1/32+ ...

So 1/10 is rounded down, and 10 * 1/10 is slightly less than 1.

As for the different results in the first and second case, that's probably because intermediate values are rounded to more digits than double has.

like image 133
MSalters Avatar answered Jan 18 '23 23:01

MSalters