Why does Perl's sprintf not round floating point numbers correctly?

前端 未结 2 1587
不思量自难忘°
不思量自难忘° 2020-12-07 02:13

I was out looking for the rounding convention used by Perl\'s built-in function sprintf.

I was thinking that it does a normal rounding (e.g. ROUND_HALF_UP as in Java

相关标签:
2条回答
  • 2020-12-07 03:02

    You have been bitten by the fact that floating point numbers are not exact representations of decimal fractions. Here's what I get:

      DB<1> $a=0.335
    
      DB<5> print sprintf("%.19f",$a)
    0.3350000000000000200
      DB<7> $b=1.335
    
      DB<8> print sprintf("%.19f",$b)
    1.3349999999999999645
      DB<9> 
    

    Since 0.335 is represented internally as slightly larger than 0.335 it rounds to .34, while 1.335 is slightly LESS than 1.335, so it rounds to 1.33.

    0 讨论(0)
  • 2020-12-07 03:10

    This is a function of IEEE floating point numbers.

    For more information, in a Perl context, see Perlfaq4 "Does Perl have a round() function" and in particular what it says about half-way-point alternation.

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