Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要找到浮点数的模数。
为此,我正在使用
NSLog(@"value >> %f",fmodf(2.0f, 0.1f));
这个的输出应该是 0.0f
但我得到的输出值>> 0.1
如何?
编译器将源文本转换0.1f为最接近的可表示值。最接近 0.1 的 IEEE-754 32 位浮点值(iOS 使用)是 0.100000001490116119384765625。
0.1f
您fmodf使用参数 2 和 0.100000001490116119384765625 进行评估。从 2 中减去后一个值的 19 倍后,余数为 0.099999971687793731689453125,即返回值。当它被四舍五入到几位显示时,结果是“0.1”。
fmodf