Bug #62959 [Nab]: Wrong math
| From: | lauris dot kuznecovs at laurisnet dot com | Date: | Fri, 31 Aug 2012 04:41:24 +0000 |
| Subject: | Bug #62959 [Nab]: Wrong math | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-173047@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62959&edit=1
ID: 62959
User updated by: lauris dot kuznecovs at laurisnet dot com
Reported by: lauris dot kuznecovs at laurisnet dot com
Summary: Wrong math
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Windows 7, Centos 5.8
PHP Version: 5.4.6
Block user comment: N
Private report: N
New Comment:
I think this can be closed. No more questions.
Previous Comments:
------------------------------------------------------------------------
[2012-08-28 19:09:35] nikic@php.net
Oh, sorry, I missed rasmus second reply ;)
------------------------------------------------------------------------
[2012-08-28 19:00:48] nikic@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/
------------------------------------------------------------------------
[2012-08-28 18:19:10] rasmus@php.net
Depends on your definition of correct. Like I said, computers cannot represent
floating point with perfect accuracy. -2.8421709430404 E-14 is an extremely
small number very close to 0, so it is as close to "correct" as you are going to
get.
You should probably read through this http://floating-point-gui.de/ to get up to
speed on how computers treat floating point values. This isn't a PHP-specific
thing. For example:
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> (443.292 - 193.126 - 250.166)
-2.842170943040401e-14
------------------------------------------------------------------------
[2012-08-28 18:11:12] lauris dot kuznecovs at laurisnet dot com
Ok, lets say that the round() function works as expected, but still this
calculation (443.292 - 193.126 - 250.166) return: -2.8421709430404 E-14
And that is not correct!
------------------------------------------------------------------------
[2012-08-28 17:57:17] rasmus@php.net
-0 may not exist in traditional math, but it very much exists in computer
representations. Computers can't represent floating point values with complete
accuracy, so if you manipulate floating point you are going to get rounding
errors. -0 means a value rounded to 0 from the negative direction. You can read
http://en.wikipedia.org/wiki/Signed_zero for
more details.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=62959
--
Edit this bug report at https://bugs.php.net/bug.php?id=62959&edit=1