简体   繁体   English

fmodf的问题,用于获取Modulo

[英]Issue with fmodf for getting the Modulo

I need to find the modulo for float numbers. 我需要找到浮点数的模。

For that, I am using 为此,我正在使用

NSLog(@"value >> %f",fmodf(2.0f, 0.1f));

The output for this should be 0.0f 此输出应为0.0f

But I am getting the output value >> 0.1 但是我得到的输出值>> 0.1

How? 怎么样?

The compiler converts the source text 0.1f to the nearest representable value. 编译器将源文本0.1f转换为最接近的可表示值。 The IEEE-754 32-bit floating-point value (which iOS uses) nearest .1 is 0.100000001490116119384765625. 最接近.1的IEEE-754 32位浮点值(iOS使用)为0.100000001490116119384765625。

The you evaluate fmodf with the arguments 2 and 0.100000001490116119384765625. 您使用参数2和0.100000001490116119384765625评估fmodf After subtracting 19 times the latter value from 2, the residue is 0.099999971687793731689453125, so that is the returned value. 从2中减去后者值的19倍后,残差为0.099999971687793731689453125,因此是返回值。 When it is rounded to a few digits for display, the result is “0.1”. 当四舍五入为几位显示时,结果为“ 0.1”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM