php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login go to bug id or search bugs for
Bug #29516 Submitted: Mathematical calculations give unexpected result 2004-08-04 05:49 UTC 2004-08-04 07:56 UTC beckwest22 at yahoo dot com Not a bug Math related 5.0.0 Windows XP No None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !

[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

```

Add a Patch

## Pull Requests

Add a Pull Request

## 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.

.
```
 Copyright © 2001-2022 The PHP Group All rights reserved. Last updated: Sat May 28 14:05:47 2022 UTC