in reply to Why the Integer part of this calculation is 102?

$ perl -e 'printf "%.20f\n", 42435.3408/411.9936' 102.99999999999998578915

This is smaller than 103, so int rounds down to 102. You'll have to use proper rounding.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: Why the Integer part of this calculation is 102?
by alokam (Initiate) on Mar 22, 2010 at 21:46 UTC
    Thanks for the quick response on my issue! I am still confused, sorry! But wouldn't we get exactly 42435.3408 when we multiply 411.9936 * 103? Is the internal representation of 42435.3408 is different in perl?

      Both 3408/10000 and 9936/10000 are periodic numbers in binary, just like 1/3 is a periodic number in decimal. It would take infinite storage to store them as floats, so they're not being stored exactly.

      You can avoid the problem by using sprintf("%.0f", $x) (rounds) instead of int($x) (truncates).

      Others have answered you already; I just want to add that in Perl 6 your code does what you expect. It achieves that by storing decimal numbers as fractions/rationals internally.
      $ perl6 > say (42435.3408).perl 26522088/625 > say (42435.3408).WHAT Rat() > say 42435.3408 / 411.9936 103 > printf "%.20f\n", 42435.3408 / 411.9936 103.00000000000000000000
      Perl 6 - links to (nearly) everything that is Perl 6.
        Thank you all for your quick response on my issue. Greatly appreciate your help. -Anju