php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47633 Bcmath precision issue
Submitted: 2009-03-12 11:59 UTC Modified: 2009-03-13 18:42 UTC
From: k1ngrs at ntlworld dot com Assigned:
Status: Not a bug Package: BC math related
PHP Version: 5.2.9 OS: Windows XP Pro SP2
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:
15 - 6 = ?
Subscribe to this entry?

 
 [2009-03-12 11:59 UTC] k1ngrs at ntlworld dot com
Description:
------------
The bcmath commands do not seem to work for numbers smaller than 0.0001
for exmaple if you add 0.00001 and 0.00001 the result is zero.
Even setting the bcscale beforehand does not fix the issue.



Reproduce code:
---------------
<?php
$a =bcadd(0.00011,0.00011,6);
$b =bcadd(0.00001,0.00001,6);
$c =bcsub((double)0.00001,(double)0.000005,6);
echo "1. 0.00011 + 0.00011 = $a OK\r\n";
echo "2. 0.00001 + 0.00001 = $b Wrong\r\n";
echo "3. 0.00001 - 0.000005 = $c Wrong\r\n";
?>

Expected result:
----------------
1. 0.00011 + 0.00011 = 0.000220 
2. 0.00001 + 0.00001 = 0.00002 
3. 0.00001 - 0.000005 = 0.000005

Actual result:
--------------
1. 0.00011 + 0.00011 = 0.000220 OK
2. 0.00001 + 0.00001 = 0.000000 Wrong
3. 0.00001 - 0.000005 = 0.000000 Wrong

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-12 22:32 UTC] k1ngrs at ntlworld dot com
I think I have uncovered the reason for the issue and that is that the function expects a string and I have passed a floating point number which OK to a point. Once you go smaller than 0.0001 you transfer a scientific notation to the function that does not compute.
So the answer is to convert it first using number_format.
Regards
Rob
 [2009-03-13 18:42 UTC] jani@php.net
You answered yourself, passing strings is the proper way.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 23:01:29 2024 UTC