質問

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.

役に立ちましたか?

解決

Using the ceilf operator fixed it for me:

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top