Bug #71136 [Opn->Nab]: periodic float numbers bug, works in php 5.6

From: Date: Wed, 16 Dec 2015 17:56:19 +0000
Subject: Bug #71136 [Opn->Nab]: periodic float numbers bug, works in php 5.6
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197936@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71136&edit=1

 ID:                 71136
 Updated by:         ab@php.net
 Reported by:        djbrainnrg at gmail dot com
 Summary:            periodic float numbers bug, works in php 5.6
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   Windows10 64bit / Apache 2.4.x
 PHP Version:        7.0.0
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2015-12-16 15:42:29] 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 15:29:40] laruence@php.net

seems all php versions behavior same: https://3v4l.org/BrWp6

------------------------------------------------------------------------
[2015-12-16 14:56:56] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71136&edit=1


Thread (6 messages)

« previous php.bugs (#197936) next »