php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71136 periodic float numbers bug, works in php 5.6
Submitted: 2015-12-16 14:56 UTC Modified: 2015-12-16 18:18 UTC
From: djbrainnrg at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.0.0 OS: Windows10 64bit / Apache 2.4.x
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: djbrainnrg at gmail dot com
New email:
PHP Version: OS:

 

 [2015-12-16 14:56 UTC] djbrainnrg at gmail dot com
Description:
------------
Floating number precision in PHP 7 seems to be buggy or at least different to PHP 5.6.
A simple calculation with a periodic number see testscript.
This happens not with all periodic numbers.
In PHP 5.6.x this works fine and output "8.2" as expected.
In PHP 7.x this does NOT work and always output "8.199999999999", doesn't matter if you use round() with precision or not.

Test script:
---------------
ini_set("precision", (PHP_INT_SIZE == 4) ? 14 : 16);
$nr = 8 + (1/10) + (9/90);
var_dump($nr, round($nr, 2));

Expected result:
----------------
"8.2" for the rounded result on all systems.

Actual result:
--------------
"8.1999999999999" also with round() called

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-16 15:29 UTC] laruence@php.net
seems all php versions behavior same: https://3v4l.org/BrWp6
 [2015-12-16 15:30 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2015-12-16 15:42 UTC] djbrainnrg at gmail dot com
-Status: Feedback +Status: Open
 [2015-12-16 15:42 UTC] djbrainnrg at gmail dot com
Yep, on Linux 64bit this is true.
On Windows 64bit not, here the PHP_INT_SIZE has changed.
For 5.6 this is 4, for PHP 7.0.0 this is 8

I guess, it's intended to have a higher INT_SIZE for PHP 7 because of 64bit.

But is the behaviour generally a bug that round() will not work with higher precision, like here with 16 instead of 14? When you put in 14 it works like a charm.
https://3v4l.org/Wr7MD
 [2015-12-16 17:56 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2015-12-16 17:56 UTC] ab@php.net
Thanks for the report. @djbrainnrg, to the first part with PHP7 - yes, PHP_INT_SIZE will now be 8 on x64 on Windows. Use precision 16 and 5.6 will deliver the same. So it is hardly a bug from this POV.

With the precision - fe here https://msdn.microsoft.com/en-us/library/6bs3y5ya.aspx . On Windows the magic number is 15 for DBL_DIG, otherwise happens exactly what you show - the precision loss. Some platforms can handle slightly more like 16 or 17 decimal places. But it is highly dependent on the particular platform, so not a bug in PHP as well.

Thanks.
 [2015-12-16 18:18 UTC] djbrainnrg at gmail dot com
Ok thank your for information.
Than i need to report this on another place.
The popular PHPExcel uses this behaviour, so this effectively had cost as a lot of headaches to find that "bug".

Could be closed, i guess.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 12:01:29 2024 UTC