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

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?
  • Comment on Re^2: Why the Integer part of this calculation is 102?

Replies are listed 'Best First'.
Re^3: Why the Integer part of this calculation is 102?
by almut (Canon) on Mar 22, 2010 at 21:58 UTC
Re^3: Why the Integer part of this calculation is 102?
by ikegami (Patriarch) on Mar 22, 2010 at 21:56 UTC

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

Re^3: Why the Integer part of this calculation is 102?
by moritz (Cardinal) on Mar 22, 2010 at 22:18 UTC
    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