php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40924 round() precision problem
Submitted: 2007-03-26 16:52 UTC Modified: 2007-03-28 13:43 UTC
From: info at e-mhd dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.2.1 OS: Win & Linux
Private report: No CVE-ID: None
 [2007-03-26 16:52 UTC] info at e-mhd dot com
Description:
------------
Problem with function round()
rounded value is incorrect

Reproduce code:
---------------
ini_set("precision", 15);
bcscale(5);
$a1 = (string)(3845349 * 34.065);
$a2 = (float)(3845349 * 34.065);
$a3 = bcmul(3845349, 34.065);
echo "A1 = ";
var_dump($a1);
echo("<br />");
echo "A2 = ";
var_dump($a2);
echo("<br />");
echo "A3 = ";
var_dump($a3);
echo "<br />Values after round() :";
echo "<br />A1 = ".round(  $a1 ,2);
echo "<br />A2 = ".round(  $a2 ,2);
echo "<br />A3 = ".round(  $a3 ,2);

Expected result:
----------------
A1 = string(13) "130991813.685" 
A2 = float(130991813.685) 
A3 = string(13) "130991813.685" 
Values after round() :
A1 = 130991813.69
A2 = 130991813.69
A3 = 130991813.69

Actual result:
--------------
A1 = string(13) "130991813.685" 
A2 = float(130991813.685) 
A3 = string(13) "130991813.685" 
Values after round() :
A1 = 130991813.69
A2 = 130991813.68
A3 = 130991813.69

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-26 17:11 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.

.
 [2007-03-28 13:35 UTC] info at e-mhd dot com
Thank you for your reply.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 01:01:30 2024 UTC