Bug #66450 [Nab]: Casting E notation strings to floats results in incorrect value
| From: | requinix@php.net | Date: | Thu, 09 Jan 2014 18:38:57 +0000 |
| Subject: | Bug #66450 [Nab]: Casting E notation strings to floats results in incorrect value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183677@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66450&edit=1
ID: 66450
Updated by: requinix@php.net
Reported by: howard at zedcore dot com
Summary: Casting E notation strings to floats results in
incorrect value
Status: Not a bug
Type: Bug
Package: *Programming Data Structures
Operating System: Debian Gnu/Linux "Wheezy" 7.2
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
To be clear it's the precision that matters here, but if that's a concern then you should
consider the impact of serialize_precision too.
Previous Comments:
------------------------------------------------------------------------
[2014-01-09 18:34:43] requinix@php.net
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/
Thank you for your interest in PHP.
The exact values depend on your precision and serialize_precision settings.
$ php -i | grep precision
precision => 14 => 14
serialize_precision => 17 => 17
$ php -r 'var_dump(...);'
float(2.6881171418161E+43)
string(19) "2.6881171418161E+43"
bool(false)
float(3.5652673131419E+29)
$ php -d precision=20 -d serialize_precision=25 -r 'var_dump(...);'
float(2.6881171418161356094E+43)
string(25) "2.6881171418161356094E+43"
bool(true)
float(0)
------------------------------------------------------------------------
[2014-01-09 17:46:31] howard at zedcore dot com
It seems to affect any E notation string being cast to float.
var_dump(exp(10), (string)exp(1), exp(1) == (string)exp(1), exp(1) - (string)exp(1));
Result:
float(2.718281828459)
float(2.718281828459)
bool(false)
float(4.5297099404706E-14)
------------------------------------------------------------------------
[2014-01-09 17:40:43] howard at zedcore dot com
Description:
------------
Casting a very large float into a string and back-again results in a different value.
The string representation of both values from var_dump() is the same, and as expected.
Reproducible on PHP 5.4.4 and PHP 5.3.10 (Not able to upgrade to the latest PHP release at work but
I'll try to reproduce at home soon).
Test script:
---------------
var_dump(exp(100), (string)exp(100), exp(100) == (string)exp(100), exp(100) - (string)exp(100));
Expected result:
----------------
float(2.6881171418161E+43)
float(2.6881171418161E+43)
bool(true)
float(0)
Actual result:
--------------
float(2.6881171418161E+43)
float(2.6881171418161E+43)
bool(false)
float(3.5652673131419E+29)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66450&edit=1