UNEXPLAINABLE

I have:

printf("%f", ceil(1/2)); //0.0000

Can somebody explain me this outcome. I have something similar in my program end it cause everything to go wrong
i tried:
printf("%f", ceil(1/(2+0.0))); //1.0000
worked but still ...
1/2 is performed using integer math since both 1 and 2 are integers.

2+0.0 is a floating point expression (due to the 0.0) and therefore
the division is performed using floating point (the denominator is
a floating point result).
is there other way to do it except adding 0.0?
Yes. printf( "%f", ceil(1.0/2.0) ); //1.0000

More example:
1
2
3
1.0  //double
1.0f //float
1.0L //long double 
another similar problem:

int z;
z=3;
printf("%d", (int)pow(10,z-1)); //99

what is going on
Floating Point Arithmetic
http://docs.python.org/tutorial/floatingpoint.html

[edit]
this works for me
printf("%d", (int)pow(10.0,z-1)); //100
Last edited on
Topic archived. No new replies allowed.