Does scientific notation affect Perl's precision?

前端 未结 1 472
梦谈多话
梦谈多话 2021-01-21 11:14

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


        
1条回答
  •  不思量自难忘°
    2021-01-21 11:27

    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.

    0 讨论(0)
提交回复
热议问题