ID: 49715 Updated by: [email protected] 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
