#49715 [Bgs]: float to string conversions
| From: | terrafrost at gmail dot com | Date: | Thu, 01 Oct 2009 16:58:03 +0000 |
| Subject: | #49715 [Bgs]: float to string conversions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-143147@lists.php.net to get a copy of this message | ||
ID: 49715
User updated by: terrafrost at gmail dot com
Reported By: terrafrost at gmail dot com
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 5.2.11
New Comment:
Maybe you should reread my bug report instead of giving knee jerk
reactions. Do you even know what a mantissa is? If you hit Ctrl + F5
on the webpage you cited, you'll come across that term.
Anyway, if the mantissa has 53 bits of precision, why is PHP only
showing 48 bits? Oh - but because the precision is "limited", I guess
that's not really a problem, is it?
You know what else has "limited precision"? 32-bit integers. Even
though those support numbers from −2,147,483,648 to 2,147,483,647,
I guess it's not really a problem if PHP's echo statement was only
capable of showing numbers ranging from, say, -128 to 127. After all,
why waste an opportunity to arbitrarily and artificially reduce
precision for no real reason what-so-ever! And if anyone complains,
just remind them that they, too, have a "limited precision"!
Previous Comments:
------------------------------------------------------------------------
[2009-10-01 16:14:13] jani@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://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
------------------------------------------------------------------------
[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