php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #556 Arithmetic error when converting variable types
Submitted: 1998-07-15 13:54 UTC Modified: 1998-07-15 15:02 UTC
From: khapeman at skidmore dot edu Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.1 OS: Solaris 2.5.1
Private report: No CVE-ID: None
 [1998-07-15 13:54 UTC] khapeman at skidmore dot edu
Run this script using the default value provided when you "submit" it:

<title>Test</title>
<center>
<h1>Test</h1>
<?
if ($posted):
  $d2 = 1999;
  $d1 = intval($d1*100);
  if ($d1 == $d2):
    echo "They are equal";
  else:
    echo "$d1 ", gettype($d1), " is not equal to $d2 ", gettype($d2);
  endif;
else:
  $d1 = 1999;
  $d1 = sprintf("%.2f",$d1/100);
  echo "<form method=post action=test.html>";
  echo "<input type=input name=d1 value=$d1>";
  $posted = 1;
  echo "<input type=hidden name=posted value=$posted>";
  echo "<input type=submit value=submit></form>";
endif;
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1998-07-15 15:02 UTC] zeev
This is directly due to the limited accuracy of floating
point numbers.  There's nothing you can do about it,
except for changing your logic.  Instead of converting
the result to an integer (which truncates any decimal digits,
in this case it truncates something like 1998.999999998 to
1998) use rounding - e.g.,
round(1998.9999999998) == 1999

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 13:01:29 2024 UTC