#49715 [NEW]: float to string conversions
| From: | terrafrost at gmail dot com | Date: | Tue, 29 Sep 2009 20:52:56 +0000 |
| Subject: | #49715 [NEW]: float to string conversions | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-143059@lists.php.net to get a copy of this message | ||
From: terrafrost at gmail dot com
Operating system: Windows XP
PHP version: 5.2.11
PHP Bug Type: Scripting Engine problem
Bug description: float to string conversions
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 bug report at http://bugs.php.net/?id=49715&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49715&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49715&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49715&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=49715&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49715&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=49715&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=49715&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=49715&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=49715&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=49715&r=support
Expected behavior: http://bugs.php.net/fix.php?id=49715&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=49715&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=49715&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=49715&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49715&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49715&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=49715&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=49715&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=49715&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=49715&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=49715&r=mysqlcfg