Difference between sheets

I have a strange problem. Here is part of my code:

int temp=1100;
int foo=floor(0.03*temp);
int foo1=0.03*temp;
if(foo-foo1){
        cout<<foo<<endl;
        cout<<foo1<<endl;
}

If 3% of temp= integer, then it foodiffers from foo1by 1.
For example:
1100 * 0.03 = 33.
foo= 33
foo1= 32.
Also, if I write it like this:

int foo=floor(0.03*1100);
int foo1=0.03*1100;

There is no such problem.
Why?

+4
source share
1 answer

Floating point numbers have problems displaying decimals. It is rather an approximation.

2 ** - 6 + 2 ** - 7 + 2 ** - 8 + 2 ** - 9 + 2 ** - 11 + 2 ** - 13 + 2 ** - 14 + 2 ** - 15 + 2 * * - 20 + 2 ** - 22 + 2 ** - 26 = 0.0299999863

, 26 . 0,03, . 0,03, (.. 0.03000001. , 0.03 .

0.03 . int . , . , 32.99999, int 32.

+2

Source: https://habr.com/ru/post/1664850/


All Articles