Bug #62845 [Com]: round returns -0
| From: | hanskrentel at yahoo dot de | Date: | Fri, 14 Nov 2014 14:34:15 +0000 |
| Subject: | Bug #62845 [Com]: round returns -0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188601@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62845&edit=1
ID: 62845
Comment by: hanskrentel at yahoo dot de
Reported by: bradley dot lee80 at googlemail dot com
Summary: round returns -0
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux (Ubuntu)
PHP Version: 5.3.16
Block user comment: N
Private report: N
New Comment:
If the -0 is expected, then this indeed was wrong in some PHP versions:
<?php
$value = -2.2204460492503E-16;
$round = round($value, 4);
var_dump($round);
Output for 4.3.0 - 5.2.0, 5.3.4 - 5.3.6, hhvm-3.0.1 - 3.3.1
float(0)
Output for 5.2.1 - 5.3.3, 5.3.7 - 5.6.3, php7@20140507 - 20141101
float(-0)
see http://3v4l.org/OcEso
Previous Comments:
------------------------------------------------------------------------
[2012-08-17 12:24:48] rasmus@php.net
Computers use a standard described in IEEE-754 to handle floating point values.
IEEE-754 uses -0 to describe values that approach 0 from a negative value. You
will see the same thing in any language that uses the computer's built-in
floating point mechanism. C, Python, Ruby, Perl, etc. For example in Python:
>>> round(-0.1)
-0.0
You can read more about it at:
http://en.wikipedia.org/wiki/Signed_zero
------------------------------------------------------------------------
[2012-08-17 12:22:16] nikic@php.net
Could you elaborate on why this would not be the expected behavior?
Also zero and negative zero are defined to compare equal, so I'm not sure whether this makes
any difference in practice.
------------------------------------------------------------------------
[2012-08-17 09:30:23] bradley dot lee80 at googlemail dot com
Description:
------------
---
From manual page: http://www.php.net/function.round#refsect1-function.round-description
---
The round function returns -0 if the value is a negative value that should round to 0
Test script:
---------------
<?php
echo round(-0.1);
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62845&edit=1