Re: Proposal: change precision for output functions
| From: | Lauri Kenttä | Date: | Tue, 08 Jan 2019 18:26:59 +0000 |
| Subject: | Re: Proposal: change precision for output functions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103702@lists.php.net to get a copy of this message | ||
On 2019-01-07 21:17, Legale.legale wrote:
I think your solution by changing precision is not good enough because float summation is still not working properly. <?php var_dump(0.1 + 0.7); returns: 0.7999999999 expected: 0.8As you may know, most computers use binary numbers. This applies to floating-point values as well. They have a limited number of binary digits. Many base-10 numbers cannot be expressed exactly in base-2 with limited number of digits. When you write 0.1 + 0.7, what you get (with double-precision floats) is closer to 0.1000000000000000055511 + 0.6999999999999999555911 = 0.7999999999999999333866. This is expected and proper and well known. You have to use formatting functions like number_format if you need neatly rounded base-10 output. There are also a lot of libraries for precise base-10 calculations. They will be a lot slower than native binary floating-point calculations, though. Regards, Lauri Kenttä