php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51233 Wrong results with float arithmetics
Submitted: 2010-03-08 11:57 UTC Modified: 2010-03-08 14:52 UTC
From: daniel dot seif at castex dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Multiple
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: daniel dot seif at castex dot de
New email:
PHP Version: OS:

 

 [2010-03-08 11:57 UTC] daniel dot seif at castex dot de
Description:
------------
When calculating with and rounding float values, the result of the calculation is wrong.

This bug seems to affect multiple versions of php. I have tested it with PHP 5.3.2 (Fedora 12), PHP 5.2.6 (Red Hat), PHP 5.3.0 (Windows XP).

Test script:
---------------
echo floor(138.95 * 100), "\n";
echo floor(141.95 * 100), "\n";
echo floor(142.95 * 100), "\n";

echo intval(142.95 * 100), "\n";
echo (int)(138.95 * 100);

Expected result:
----------------
13895
14195
14295
14295
13895

Actual result:
--------------
13894
14194
14294
14294
13894

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-08 12:12 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-03-08 12:12 UTC] johannes@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.

If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.
 [2010-03-08 13:20 UTC] daniel dot seif at castex dot de
Thank you for the quick reply.

The string representation is not the problem here, though. 

We are calculating with monetary data and converting from one currency to another. 

Imagine a two products, worth 138.00 and 0.95 with the need to keep only 2 decimal digits:

$totalPrice = floor((138.00 + 0.95) * 100) / 100;

The result should be 138.95, but PHP returns 138.94 ...

I consider this to be a problem on a worse level than just 'bogus'

Best Regards
 [2010-03-08 13:33 UTC] aharvey@php.net
Due to their limited precision, you shouldn't use floating-point values for currency calculations. Ever. This is well established best practice across a variety of programming languages.

You should use either integers to represent the value in cents (or whatever your smallest currency unit is) or an arbitrary precision library such as BCMath.

Please note that this bug tracker isn't a support forum, and further questions on this would be better directed to a Web forum like Stack Overflow, the PHP general mailing list, a newsgroup, or IRC.
 [2010-03-08 14:52 UTC] pajoye@php.net
Please carefully read the link posted by Johannes. 138.95 can end to be represented as 138.4999999999999. The rest becomes obvious then.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 01:01:35 2025 UTC