#49715 [Opn->Bgs]: float to string conversions
| From: | jani@php.net | Date: | Thu, 01 Oct 2009 16:14:13 +0000 |
| Subject: | #49715 [Opn->Bgs]: float to string conversions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-143137@lists.php.net to get a copy of this message | ||
ID: 49715
Updated by: jani@php.net
Reported By: terrafrost at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 5.2.11
New Comment:
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://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2009-09-29 20:52:56] terrafrost at gmail dot com
Description:
------------
Displaying a float in base-10 when fractional values are involved is
difficult, as per <http://php.net/float>. Even if
fractional values
aren't involved, displaying the float might still be problematic, if the
number you're trying to display is to big to be stored in the mantissa
of a floating point number.
Problem is, floating point numbers aren't always displayed correctly,
even if the mantissa can hold the number in question. For example, if
you have a double precision floating point number, your mantissa has 53
bits. The following corroborates this:
<?php
$test = 1;
$bits = 0;
while (fmod($test, 2) == 1) {
$test = 2 * $test + 1;
$bits++;
}
echo $bits;
?>
That, for me, displays 53.
The problem occurs with anything utilizing 48 or more mantissa bits:
<?php
$bits = 0;
while (true) {
if (bcpow(2, $bits) != (string) pow(2, $bits)) {
break;
}
$bits++;
}
echo $bits;
?>
Reproduce code:
---------------
<?php
echo pow(2, 52);
echo "\r\n";
echo 4503599627370496;
echo "\r\n";
echo pow(2, 52) == 4503599627370496 ? 'equal' : 'not equal';
echo "\r\n";
echo pow(2, 52) == 4503599627370500 ? 'equal' : 'not equal';
?>
Expected result:
----------------
4503599627370496
4503599627370496
equal
not equal
Actual result:
--------------
4503599627370500
4503599627370500
equal
not equal
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49715&edit=1