Question

The following code is returning an unexpected (and wrong value).

x1 = ceil(ceil(ceil(100*1.0f)*1.0f)*1.1f);

It is returning 111 not 110. I am aware that multiplying a number by 1 is pointless, but those 1's are not always ones. When they are other values the function works as expected.

Was it helpful?

Solution

Using the ceilf operator fixed it for me:

float x1 = ceilf(ceilf(ceilf(100*1.0f)*1.0f)*1.1f);
NSLog(@"%f",x1); // 110

OTHER TIPS

This is a matter of floating point errors. 1.1f is probably really something like 1.100056778.

This means that 100 * 1.100056778 = 110.0056778. And the ceil of that number is 111.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top