php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66450 Casting E notation strings to floats results in incorrect value
Submitted: 2014-01-09 17:40 UTC Modified: 2014-01-09 18:38 UTC
From: howard at zedcore dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: Irrelevant OS: Debian Gnu/Linux "Wheezy" 7.2
Private report: No CVE-ID: None
 [2014-01-09 17:40 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-09 17:46 UTC] howard at zedcore dot com
-Summary: Casting extremely large E notation strings to floats results in incorrect value +Summary: Casting E notation strings to floats results in incorrect value
 [2014-01-09 17:46 UTC] 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 18:34 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-01-09 18:34 UTC] 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 18:38 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 13:03:32 2021 UTC