科学计数法会影响Perl的精度吗? [英] Does scientific notation affect Perl's precision?

查看:157
本文介绍了科学计数法会影响Perl的精度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中遇到了一个奇怪的行为.以下减法的结果应为零(在Python中是这样):

I encountered a weird behaviour in Perl. The following subtraction should yield zero as result (which it does in Python):

print 7.6178E-01 - 0.76178
-1.11022302462516e-16

为什么会发生以及如何避免呢?

Why does it occur and how to avoid it?

P.S.在为x86_64-linux-gnu-thread-multi构建的v5.10.0"(Ubuntu 9.04)和为darwin-2level构建的v5.8.9"上出现效果(Mac OS 10.6)

P.S. Effect appears on "v5.10.0 built for x86_64-linux-gnu-thread-multi" (Ubuntu 9.04) and "v5.8.9 built for darwin-2level" (Mac OS 10.6)

推荐答案

并不是说科学符号会影响精度,而不是二进制表示的浮点符号的局限性.请参阅 perlfaq4 的答案.对于任何依赖于数字存储基础体系结构的语言来说,这都是一个问题.

It's not that scientific notation affects the precision so much as the limitations of floating point notation represented in binary. See the answers to the perlfaq4. This is a problem for any language that relies on the underlying architecture for number storage.

  • Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
  • Why is int() broken?

如果您需要更好地处理数字,请查看 bignum 编译指示.

If you need better number handling, check out the bignum pragma.

这篇关于科学计数法会影响Perl的精度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆