Bug #70804 [Com]: round(-0)=-0 but +round(-0)=0
| From: | davividal at gmail dot com | Date: | Thu, 05 Nov 2015 19:00:02 +0000 |
| Subject: | Bug #70804 [Com]: round(-0)=-0 but +round(-0)=0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-197054@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70804&edit=1
ID: 70804
Comment by: davividal at gmail dot com
Reported by: davividal at gmail dot com
Summary: round(-0)=-0 but +round(-0)=0
Status: Open
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 5.6.14
Block user comment: N
Private report: N
New Comment:
After a bunch of operations I saw something like:
<?php
$var = round($foo - $bar);
$var = +$var;
?>
And wondered what was going on.
As for your statement that round() should turn the number into positive: that's what Ruby does,
but not what Python does (I only tested on these 2). So I don't know what the correct behaviour
should be here.
>>> foo = -1e-99
>>> round(foo)
-0.0
>>> +round(foo)
-0.0
irb(main):001:0> foo = -1e-99
=> -1.0e-99
irb(main):002:0> foo.round
=> 0
irb(main):003:0> +foo.round
=> 0
What I do know: +(-x) should be equal to -x. :)
Previous Comments:
------------------------------------------------------------------------
[2015-10-27 20:59:59] requinix@php.net
Changed the summary. I take it you were originally seeing this with some multiplication?
It's not that the plus sign causes it to become positive. The same thing happens with a
negative sign too: PHP recognizes the zero and forces it to be positive.
I don't know if this counts as a bug but it certainly could qualify as a request: while -0 is a
legitimate (though unusual) floating-point number, I think that round(-0) should also force it to be
positive.
------------------------------------------------------------------------
[2015-10-27 19:41:45] davividal at gmail dot com
Description:
------------
Given a negative number, it's possible to turn it into positive by just putting a positive sign
in front of it.
Test script:
---------------
var_dump(round(-1e-99));
var_dump(+round(-1e-99));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70804&edit=1