Perl 模运算符问题
为什么第一个示例打印错误的结果?
perl -le 'print $x = 100*1.15 % 5'
4
perl -le 'print $x = 1000*1.15 % 5'
0
Why does the first example print a wrong result ?
perl -le 'print $x = 100*1.15 % 5'
4
perl -le 'print $x = 1000*1.15 % 5'
0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为浮点运算。
给你114。
It's because of floating point arithmetic.
Gives you 114.
四舍五入。请记住,计算机无法完美地表示实际的小数位 - 它们只是近似值。在我的计算机上,
perl -le 'print $x = (100*1.15)-115'
给出结果-1.4210854715202e-14
,这意味着100 *1.15
几乎是 115,但不完全是 115。Rounding. Keep in mind that computers can't represent actual decimal places perfectly - they approximate. On my computer,
perl -le 'print $x = (100*1.15)-115'
gives the result-1.4210854715202e-14
, which means that100*1.15
is almost, but not quite, 115.