Search code examples
perl

Why the number decrements by one when converting to integer in Perl?


Why the number decrements by one when converting to integer in Perl?

23/20 * 100000000
=> 115000000

int(23/20 * 100000000)
=> 114999999

Why?


Solution

  • 3/20 is a periodic number in binary just like 1/3 is periodic in decimal. Specifically, 23/20 is equal to

         ____
    1.0010011 × 2^0
    

    It would take infinite resources to store this number as a floating point number. Due to limited resources, a slightly smaller number is stored instead.

    1.001 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0 × 2^0
    

    In decimal:

    $ perl -e'printf "%.100g\n", 23/20'
    1.149999999999999911182158029987476766109466552734375
    

    You then multiply this by 100000000, which results in even more loss of precision.

    $ perl -e'printf "%.100g\n", 23/20 * 100000000'
    114999999.99999998509883880615234375
    

    int performs truncation, and print performs some rounding. With truncation, you get 114999999. With rounding, you get 115000000.