Perl 模运算符问题

发布于 2024-09-11 14:21:38 字数 124 浏览 8 评论 0原文

为什么第一个示例打印错误的结果?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-09-18 14:21:38

这是因为浮点运算。

print $x = int(100*1.15);

给你114。

It's because of floating point arithmetic.

print $x = int(100*1.15);

Gives you 114.

冷了相思 2024-09-18 14:21:38

四舍五入。请记住,计算机无法完美地表示实际的小数位 - 它们只是近似值。在我的计算机上,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 that 100*1.15 is almost, but not quite, 115.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文