php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41401 Order of Operations error on divide by negative
Submitted: 2007-05-15 15:44 UTC Modified: 2007-05-18 20:49 UTC
From: drlippman at yahoo dot com Assigned:
Status: Closed Package: Math related
PHP Version: 4.4.7 OS: Windows, Linux
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: drlippman at yahoo dot com
New email:
PHP Version: OS:

 

 [2007-05-15 15:44 UTC] drlippman at yahoo dot com
Description:
------------
Left-to-right order of operations does not appear to be honored when dividing by a negative

Reproduce code:
---------------
1/-2*5

Expected result:
----------------
-2.5

Actual result:
--------------
-.1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-16 12:50 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2007-05-16 18:24 UTC] et@php.net
This IS a bug.
http://de.php.net/operators lists unary - having higher precedence than the arithmetic operators, and that is the way it should be. So, this statement should evaluate to 1/(-2)*5 and that is -2.5 and not -0.1.
 [2007-05-17 13:02 UTC] smlerman at gmail dot com
<?php

echo 1 / -2 * 5;   // -0.1
$a = 2;
echo 1 / -$a * 5;  // -0.1
$a = -2;
echo 1 / $a * 5;   // -2.5
echo 1 / -$a * 5;  // 0.1

?>

Looks like a problem with the parsing of unary negation.
 [2007-05-18 00:08 UTC] stas@php.net
Unary negation in PHP is basically 0-value (i.e. it's subtraction). The docs and the code tell difference things - the docs say unary and binary minus have different priority but in the code it's the same.
 [2007-05-18 20:49 UTC] stas@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed in 5/6, not in 4 though (due to objection from Derick). 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC