Does scientific notation affect Perl's precision?

[亡魂溺海] 提交于 2019-12-20 03:20:30

问题


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. 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)


回答1:


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?

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



来源:https://stackoverflow.com/questions/2056681/does-scientific-notation-affect-perls-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!