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
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: c dot becker at provi dot de
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sat Dec 21 17:01:58 2024 UTC