Bug #69517 [Nab]: round() does something weird on result of pow()
| From: | bwoebi@php.net | Date: | Thu, 23 Apr 2015 21:34:39 +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-192309@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: bwoebi@php.net
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:
pow(8, 2/3) is 3.9999999999999956 ⦠When you then do a round($n, 15) operation, it rounds to
3.999999999999996 which is the same as 3.9999999999999956 internally, thus returning true.
Everything with second parameter smaller than 15 rounds to float(4), which isn't equal to $n.
Previous Comments:
------------------------------------------------------------------------
[2015-04-23 20:34:59] leafface at gmail dot com
"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)
------------------------------------------------------------------------
[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