Strange results with C ++ ceiling function

I try to use the ceiling function and get some weird results. If I perform a stream operation on a decimal number times a hundred, I get a certain result. However, if I directly perform ceil on the result of this multiplication, I get a completely different result. Another twist is that these different results occur only for certain numbers. Any help would be appreciated.

#include <stdio.h>
#include <cmath>

int main ()
{
cout << "The ceiling of " << 411 << " is " << ceil(411) << endl;
cout << "The ceiling of 4.11*100  is " << ceil(4.11*100) << endl;

cout << "The ceiling of  " << 121 << " is " << ceil(121) << endl;
cout << "The ceiling of 1.21*100  is " << ceil(1.21*100) << endl;;
}


OUTPUT:

The ceiling of 411 is 411
The ceiling of 4.11*100  is 412
The ceiling of  121 is 121
The ceiling of 1.21*100  is 121
+5
source share
3 answers

, . , 4.11 4.11, - . " 4.11" 100, ceil 412, ! , , .


:

float a = 3.2; //3.2 is double!
if ( a == 3.2 )
    cout << "a is equal to 3.2"<<endl;
else
    cout << "a is not equal to 3.2"<<endl;

float b = 3.2f; //3.2f is a float. Note: f is appended to make it float!
if ( b == 3.2f )
    cout << "b is equal to 3.2f"<<endl;
else
    cout << "b is not equal to 3.2f"<<endl;

:

a 3.2. b 3.2f

ideone: http://www.ideone.com/pAGzM

a float double, . .

+9

FAQ:

[29.16] ? 0,43

#include <iostream>

 int main()
 {
   float a = 1000.43;
   float b = 1000.0;
   std::cout << a - b << '\n';
   ...
 }

: // ++; . , comp.lang.++, .

: - . IEEE 32- 1 , 8 23 . 1.xxxxx... 1 , 24 . 1000.43 ( , , , 0,1), float . 1000.43 bitpattern ( "s" , "" "m" ):

 seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
 01000100011110100001101110000101

1111101000.01101110000101 1000 + 7045/16384. +0,429992675781. 24 1 16M . (53 ).

. [29.17] ?

+5

ceil(x) x.

, (, 4.11 1.21), - . . 4.11 , 4.11*100 , 411, ceil(4.11*100) == 412 ( 412 - , , , 411), 1.21 , 1.21*100 121, ceil(1.21*100)==121.

, .

+1

All Articles