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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: khapeman at skidmore dot edu
New email:
PHP Version: OS:

 

 [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: Mon May 13 19:01:33 2024 UTC