Bug #69517 [Opn->Nab]: round() does something weird on result of pow()
| From: | cmb@php.net | Date: | Thu, 23 Apr 2015 20:08:53 +0000 |
| Subject: | Bug #69517 [Opn->Nab]: round() does something weird on result of pow() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192305@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69517&edit=1
ID: 69517
Updated by: cmb@php.net
Reported by: leafface at gmail dot com
Summary: round() does something weird on result of pow()
-Status: Open
+Status: Not a bug
Type: Bug
-Package: Unknown/Other Function
+Package: Math related
Operating System: Mac OS X
PHP Version: 5.5.24
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
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://www.floating-point-gui.de/
Thank you for your interest in PHP.
Never ever compare floating point values for equality. :)
Previous Comments:
------------------------------------------------------------------------
[2015-04-23 18:45:21] leafface at gmail dot com
Description:
------------
round() acts weird in some cases when pow() theoretically returns a round number. The bug
doesn't occur if precision for round() is set and is greater than 14.
(Sorry, I actually have PHP 5.5.14, but it's pretty unlikely this has been resolved since.)
Test script:
---------------
<pre>
<?
$n = pow(8, 2/3);
echo
($n == round($n) ? 'true' : 'false').
"\n".
($n == round($n, 14) ? 'true' : 'false').
"\n".
($n == round($n, 15) ? 'true' : 'false');
Expected result:
----------------
true
true
true
Actual result:
--------------
false
false
true
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69517&edit=1