php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62845 round returns -0
Submitted: 2012-08-17 09:30 UTC Modified: 2014-11-14 17:18 UTC
From: bradley dot lee80 at googlemail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.3.16 OS: Linux (Ubuntu)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bradley dot lee80 at googlemail dot com
New email:
PHP Version: OS:

 

 [2012-08-17 09:30 UTC] 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);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-17 12:22 UTC] 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 12:24 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-08-17 12:24 UTC] 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
 [2014-11-14 14:34 UTC] hanskrentel at yahoo dot de
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
 [2014-11-14 17:18 UTC] rasmus@php.net
0 and -0 are equivalent, so if this is causing any sort of issue in your code you need to fix your code. You can read all about rounding in PHP here:

  https://wiki.php.net/rfc/rounding
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC