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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: k1ngrs at ntlworld dot com
New email:
PHP Version: OS:

 

 [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: Sat May 11 01:01:31 2024 UTC