php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8164 weird subtraction result
Submitted: 2000-12-08 06:23 UTC Modified: 2000-12-09 02:15 UTC
From: c dot becker at provi dot de Assigned:
Status: Closed Package: Math related
PHP Version: 4.0.0 OS: SuSE Linux 6.3
Private report: No CVE-ID: 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 !
Your email address:
MUST BE VALID
Solve the problem:
8 - 2 = ?
Subscribe to this entry?

 
 [2000-12-08 06:23 UTC] c dot becker at provi dot de
I don't know, if this is a Math related problem,
it is a simple subtraction problem.

If I type this 

$one = "555.6";
$two = "555.5";

$result = $one - $two;

echo $result;

the result in my browser is

0.10000000000002

This occurs with more numbers than above.
So, what is my problem?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-12-08 11:49 UTC] c dot becker at provi dot de
this won't work, either.

$one = 555.6;
$two = 555.5;

$result = $one - $two;

echo $result;


 [2000-12-09 02:15 UTC] lyric@php.net
This is not a PHP bug, it's the age old issue of the accuracy of a floating point number. Since a computer cannot store a number *precisely*, it stores as close as it can get. When you do math operations on such a number, the difference (aka rounding error) between what you entered and what the computer stored is exagerated. See http://www.dcs.ed.ac.uk/home/mhe/plume/node9.html for a bit of an explanation.

Try using printf( "%.1f", $result );

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC