Bug #69517 [Nab]: round() does something weird on result of pow()
| From: | leafface at gmail dot com | Date: | Thu, 23 Apr 2015 20:35:00 +0000 |
| Subject: | Bug #69517 [Nab]: round() does something weird on result of pow() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192307@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
User updated by: leafface at gmail dot com
Reported by: leafface at gmail dot com
Summary: round() does something weird on result of pow()
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Mac OS X
PHP Version: 5.5.24
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
"Floating point values have a limited precision." - Actually what's really strange in
my bug report is that "The bug doesn't occur if precision for round() is set and is
greater than 14.", i.e. the bug doesn't only occur if there's no precision set for
round(), but also if the precision is less than 15, even if it's 1! Meaning this statement for
example results false: $n == round($n, 1)
Previous Comments:
------------------------------------------------------------------------
[2015-04-23 20:08:53] cmb@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://www.floating-point-gui.de/
Thank you for your interest in PHP.
Never ever compare floating point values for equality. :)
------------------------------------------------------------------------
[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