php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35656 Strange behaviour when multiplying floats with one multiplicator being 0
Submitted: 2005-12-13 11:00 UTC Modified: 2005-12-13 11:11 UTC
From: php at lipfi dot ch Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.1 OS: Gentoo Linux 2005.1
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: php at lipfi dot ch
New email:
PHP Version: OS:

 

 [2005-12-13 11:00 UTC] php at lipfi dot ch
Description:
------------
When multiplying two float values, one being 0, one being negative, it's possible to either get 0 or -0 (!) as a result depending on the operands order.

This is IMHO not how math is supposed to work.

Additionally, I'm seing discrepancies between the variables value and what var_dump() things it contains. The problem is very simple to reproduce

Reproduce code:
---------------
<?php
  $v = 0*-0.02;
  var_dump($v);
  echo "$v\n";
  $v = floatval($v);
  echo "$v\n\n";

  $v = -0.02*0;
  var_dump($v);
  echo "$v\n";
?>

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

float(0)
0

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

float(0)
0

(the -0 obviously being the problem here)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-13 11:02 UTC] sniper@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2005-12-13 11:10 UTC] php at lipfi dot ch
I fear your decision to mark this as bogus was a very quick one. 

http://www.php.net/manual/en/language.operators.arithmetic.php

does not tell me anywhere that the commutative law is not valid in PHP. 

Also note that I'm very well aware of floats having limited precision, but -0 is no defined value and certainly not something you'd expect to get as a result of a multiplication. And certainly not depending on the operands order.

0*0.02 = -0
0.02*0 = 0

where var_dump() certainly knows that -0 == 0, but the engines core seems not to.

I certainly expect the arithmetics in PHP to work like it's supposed to. This means adhering to the commutative law which basically means that I can interchange the operands in a multiplication.

Besides: -0 is IMHO not a valid value. It's either 0 or not 0, but not -0.

I'd really appreciate a further comment of why the behaviour whitnessed here is really working as intended and where that is documented in the manual.

Thank you

Philip
 [2005-12-13 11:11 UTC] php at lipfi dot ch
make that example calculation

0*-0.02 = -0
-0.02*0 = 0

Sorry. I forgot the - sign in the previous post.

Philip
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Sep 14 17:00:01 2025 UTC