php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35275 Addition of some negative floats fails
Submitted: 2005-11-18 14:13 UTC Modified: 2005-11-18 16:39 UTC
From: uwe dot voelker at vline dot de Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.0.5 OS: Win XP
Private report: No CVE-ID: None
 [2005-11-18 14:13 UTC] uwe dot voelker at vline dot de
Description:
------------
Addition of some negative floats fails !


Reproduce code:
---------------
<?php
// PHP 5.05
// First example shows the bug
if ( (-56.33) != ((-14.13) + (-42.20)) ) {
  print "<HR> First Example: Why is -56.33 not -56.33 ??? <HR>";
}
else
{
  print "<HR> First Example: OK <HR>";
}
// Second example with modified values but same result is ok
if ( (-56.33) != ((-14.12) + (-42.21)) ) {
print "<HR> Second Example: Why is -56.33 not -56.33 ??? <HR>";
}
else
{
  print "<HR> Second Example: OK <HR>";
}
?>


Expected result:
----------------
First example:  true, but must be false.
Second example: false, it is OK.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-18 14:59 UTC] derick@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.

.
 [2005-11-18 16:39 UTC] uwe dot voelker at vline dot de
OK, after reading ten pages about float i take
the ROUND function, it works now.

if ( (-56.33) != Round((-14.13) + (-42.20),2) ) {
  print "<HR> First Example: Why is -56.33 not -56.33 ??? <HR>";
}
else
{
  print "<HR> First Example: OK <HR>";
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC