-
-
Notifications
You must be signed in to change notification settings - Fork 7
Why is 14 the magic precision number? #13
Comments
I admit that the code comment "14 is the magic precision number" is very mysterious. I should probably update that with more explanation. According to the PHP manual page for the
So, the default floating-point precision in PHP is 14 (unless this is changed in php.ini). Consider the following (using psysh):
The two places where this is used in BigNumber is in the |
This library uses bcmath, and this is an unfortunate problem with any kind of arbitrary precision arithmetic. I recommend reading http://floating-point-gui.de/ for more information on what's going on. This should illustrate the problem, though: >>> $value = bcdiv(1, 3, 2);
=> "0.33"
>>> bcmul($value, 0.15, 2);
=> "0.04"
>>> $value = bcdiv(1, 3, 50);
=> "0.33333333333333333333333333333333333333333333333333"
>>> bcmul($value, 0.15, 50);
=> "0.04999999999999999999999999999999999999999999999999" |
Originally posted at moontoast#9, @lhfeng asks:
The text was updated successfully, but these errors were encountered: