#44277 [Bgs]: when round() rounds a number -1<x<0, then it returns -0 (should be 0)

From: Date: Tue, 04 Mar 2008 11:55:09 +0000
Subject: #44277 [Bgs]: when round() rounds a number -1<x<0, then it returns -0 (should be 0)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-122910@lists.php.net to get a copy of this message
ID: 44277 User updated by: felix dot ospald at gmx dot de Reported By: felix dot ospald at gmx dot de Status: Bogus Bug Type: Math related Operating System: Windows XP PHP Version: 5.2.5 New Comment: Yes I was aware of the limited precision. But I'm still wondering if it is a good idea to have a positive and a negative zero as output of rounding operations that both should be mathematically the same zero which has noting to do with a limited float precision. Rounding is not like taking a left or right limit to the next integer. It also inherits problems in some cases e.g.: echo (((round(0.1).'EUR') == (round(-0.1).'EUR')) ? 'EQUAL' : 'NOT EQUAL'); gives: "NOT EQUAL" MATLAB for example also doesn't have a negative zero here. >> digits(32) >> sym(-0.1,'d') ans = -.10000000000000000555111512312578 >> sym(round(-0.1),'d') ans = 0 >> Previous Comments: ------------------------------------------------------------------------ [2008-03-03 13:16:22] jani@php.net # php -n -dprecision=32 -r '$a=-0.1; var_dump($a);var_dump(round($a));' float(-0.10000000000000000555111512312578) float(-0) ------------------------------------------------------------------------ [2008-03-03 13:15:39] jani@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://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. ------------------------------------------------------------------------ [2008-02-28 00:28:34] felix dot ospald at gmx dot de Description: ------------ This bug was already reported for Linux 3405 in 2000. Is this a bug or feature? Reproduce code: --------------- echo round(-0.1); Expected result: ---------------- "0" Actual result: -------------- "-0" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=44277&edit=1

« previous php.bugs (#122910) next »