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

Time:04-29

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

23/20 * 100000000
=> 115000000

int(23/20 * 100000000)
=> 114999999

Why?

CodePudding user response:

The subexpression "23/20", evaluated first, promotes the whole expression to floating point, and so

sprintf( "0.15f", 23/20 * 100000000)

yields

114999999.999999985098839

while the equivalent

sprintf( "0.15f", 23 * 100000000 / 20)

evaluates "23 * 100000000" first, which is evenly divisible by 20, and yields

115000000.000000000000000

CodePudding user response:

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. When truncation, you get 114999999. With rounding, you get 115000000.

  •  Tags:  
  • perl
  • Related