php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62170 Inaccurate division
Submitted: 2012-05-27 05:01 UTC Modified: 2012-05-27 14:33 UTC
From: cidahf at live dot co dot uk Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.4.3-4~precise+1 OS: Ubuntu 12.04
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: cidahf at live dot co dot uk
New email:
PHP Version: OS:

 

 [2012-05-27 05:01 UTC] cidahf at live dot co dot uk
Description:
------------
When I divide 25.4 by 10000 I get an awkward 
0.002539999999999999723276911112179732299409806728363037109375 instead of the 
desired 0.00254. I tried the same with Ruby 1.8.7 and it returned the correct 
result.

This issue is problematic for unit conversion.

I also got this in 5.3 but I upgraded before making a report.

Test script:
---------------
echo 25.4 / 10000;

Expected result:
----------------
0.00254

Actual result:
--------------
0.002539999999999999723276911112179732299409806728363037109375

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-27 05:14 UTC] cidahf at live dot co dot uk
-PHP Version: 5.4.3 +PHP Version: 5.4.3-4~precise+1
 [2012-05-27 05:14 UTC] cidahf at live dot co dot uk
Changed the version to the exact value returned from PHP_VERSION.
 [2012-05-27 14:33 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-05-27 14:33 UTC] rasmus@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://www.floating-point-gui.de/

Thank you for your interest in PHP.

All languages have this issue when using the native floating point mechanism and 
displaying it with high precision. By default PHP will give you the right 
result:

php > echo 25.4/1000;
0.0254

But, if you increase the precision you will see it is just an approximation 
because computers can't actually make that division precisely:

php > ini_set('precision',32);
php > echo 25.4/1000;
0.025399999999999998967492587098604
 [2012-05-28 05:18 UTC] cidahf at live dot co dot uk
I understand now. I'm getting more accurate results now that I have lowered the 
precision down to 14.

Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC