php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52355 Negating zero does not produce negative zero
Submitted: 2010-07-16 08:31 UTC Modified: 2015-12-17 22:26 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: spoon dot reloaded at gmail dot com Assigned:
Status: Closed Package: Math related
PHP Version: 5.3.2 OS:
Private report: No CVE-ID: None
 [2010-07-16 08:31 UTC] spoon dot reloaded at gmail dot com
Description:
------------
Using the unary negation operator on a float 0 (positive zero) value, simply returns positive zero again, instead of -0 (negative zero), like it does in other languages.
var_dump(-(0.));
float(0)

The negative zero exists and can be displayed properly, so that is not the problem:
var_dump(-1/INF);
float(-0)
But for some reason there is no easy way to access it.

Test script:
---------------
var_dump(-(0.));
var_dump(-1/INF);


Expected result:
----------------
float(-0)
float(-0)

Actual result:
--------------
float(0)
float(-0)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-16 08:34 UTC] spoon dot reloaded at gmail dot com
Nor can you specify -0 as a float literal:
var_dump(-0.);
float(0)
 [2012-02-13 08:31 UTC] salsi at icosaedro dot it
The zero negative could appear also in the simplest expressions, yielding quite unexpected results, as in

$zn = -1 * 0.0;
echo $zn; ==> -0

Under PHP several int expressions might result in a float value, and most programmers are unaware that their "exact" calculations are made in the quite obscure domain of the approximated floating-point numbers, possibly with safety and security related issues.

Since $zn==0.0 and $zn===0.0 are both true, the only way I have found to detect the zero negative value is by comparing its representation as a string:

if( $f === 0.0 and "$f" === "-0" )
    echo "WARNING: it's zero negative!";
 [2013-06-26 15:51 UTC] dlsniper@php.net
I've found that this break was introduced in 5.2.3, http://3v4l.org/MFa5s 
I'll try and investigate further.
 [2015-12-17 22:26 UTC] ajf@php.net
This is a bug, marking as such.

Still exists in PHP 7, annoyingly.
 [2015-12-17 22:26 UTC] ajf@php.net
-Type: Feature/Change Request +Type: Bug
 [2015-12-17 22:40 UTC] ajf@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a4648ded430985e019b446939c4ff5bef36c0b91
Log: Fix bug #52355
 [2015-12-17 22:40 UTC] ajf@php.net
-Status: Open +Status: Closed
 [2015-12-17 22:41 UTC] ajf@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a4648ded430985e019b446939c4ff5bef36c0b91
Log: Fix bug #52355
 [2015-12-17 23:36 UTC] ajf@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4e01269082d20f5598c481f122b3eea10a586e2b
Log: Fix tests broken by 'Fix bug #52355'
 [2015-12-17 23:36 UTC] ajf@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4e01269082d20f5598c481f122b3eea10a586e2b
Log: Fix tests broken by 'Fix bug #52355'
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4e01269082d20f5598c481f122b3eea10a586e2b
Log: Fix tests broken by 'Fix bug #52355'
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of ajf@ajf.me
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a4648ded430985e019b446939c4ff5bef36c0b91
Log: Fix bug #52355
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC