Coder-Spirit / php-bignumbers

A robust library to handle immutable big numbers inside PHP applications
MIT License
131 stars 29 forks source link

Incorrect decimal calculation. #69

Closed kevindecapite closed 4 years ago

kevindecapite commented 4 years ago

Using PHP 7.3.6 I get the following results using PHP "straight math":

php > echo 156 + (44 / 60) + (31 / 3600);
156.74194444444

Using this library (same system and PHP version), I get:

156.7386

On my iPhone and Mac, I get the same value as this library. However, on a Microsoft Surface Go tablet, Google's online calculator and an Android phone, I get:

156.741944444

Google's Calculator https://www.google.com/search?q=online+calculator

kevindecapite commented 4 years ago

A developer friend helped me with this and discovered that the level of precision used in calculations is based on the precision of the numbers passed into create(), unless specified explicitly in the 2nd parameter. If I run the same calculation but specify the scale to 16, then I get the 156.741944444 value while using this library.

Also, adjunct to this comment is that the latest version of macOS Catalina now calculates the 156.741944444 value as well and so does iPhone calculator if running it in scientific mode.