php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29516 Mathematical calculations give unexpected result
Submitted: 2004-08-04 05:49 UTC Modified: 2004-08-04 07:56 UTC
From: beckwest22 at yahoo dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.0.0 OS: Windows XP
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: beckwest22 at yahoo dot com
New email:
PHP Version: OS:

 

 [2004-08-04 05:49 UTC] beckwest22 at yahoo dot com
Description:
------------
I have been reading that doing some mathematical calculations with floats and integers in php gives some unexpected results.  I am not sure if this is a bug or not. When I do a combination of division and subtraction with the number 19, I get unexpected results.  I would appreciate any light you could shine on this subject.

Modules compiled with PHP: 
I did not compile any modules with PHP.

The only thing different in my php.ini file is that I uncommented the extension=php_mysql.dll line in the file.



Reproduce code:
---------------
<?php
$num = 19;
$res = $num/10;
echo $res;
echo "<br>";
$res1 = ($num%10)/10;
echo $res1;
echo "<br>";
$res2 = $res - $res1;
echo $res2;
$final_result = $res2 - 1;
echo "<br>";
echo $final_result;
?>


Expected result:
----------------
This is what I expect from the calculations:

$res = 19/10; // equals 1.9
$res1 = (19%10)/10; // equals 0.9
$res2 = 1.9 - 0.9; // equals 1
$final_result = 1 - 1; // equals 0

expected $final_result to be 0

Actual result:
--------------
$final_result equals -1.11022302463E-016

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-04 07:56 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.
 
Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC