'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 1:[1]
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.
Solution 2:[2]
The subexpression "23/20", evaluated first, promotes the whole expression to floating point, and so
sprintf( "%30.15f", 23/20 * 100000000)
yields
114999999.999999985098839
while the equivalent
sprintf( "%30.15f", 23 * 100000000 / 20)
evaluates "23 * 100000000" first, which is evenly divisible by 20, and yields
115000000.000000000000000
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tom Williams |
