UNEXPLAINABLE

May 7, 2010 at 10:02pm
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
May 7, 2010 at 10:06pm
i tried:
printf("%f", ceil(1/(2+0.0))); //1.0000
worked but still ...
May 8, 2010 at 3:23am
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).
May 8, 2010 at 5:09am
is there other way to do it except adding 0.0?
May 8, 2010 at 7:56am
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 
May 9, 2010 at 1:00pm
another similar problem:

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

what is going on
May 9, 2010 at 3:13pm
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 May 9, 2010 at 3:19pm
Topic archived. No new replies allowed.