note 40686 added to ref.bc
| From: | george at whiffen dot net | Date: | Thu, 11 Mar 2004 12:17:45 +0000 |
| Subject: | note 40686 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66630@lists.php.net to get a copy of this message | ||
More exponential gotchas:
Even if you think your number is not in exponential notation, php may change it to exponential
before passing it to bc e.g.
bcadd(100000000000000,1) returns 1
bcadd(0.00001,0.00001,5) returns 0
The first example can be solved by increasing the value of php's precision configuration
variable, e.g. ini_set('precision',20). This does not work for the second example, which
requires you to do the string conversion yourself using the "f" type of sprintf() e.g.
bcadd(sprintf("%.5f",0.00001),sprintf("%.5f",0.00001),5)
However, be aware that the "f" type of printf/sprintf is limited as to number of places
i.e. this won't work for 1e-40.
It's probably best to avoid using numbers with bc altogether and just keep everything as string
e.g. bcadd('0.00001','0.00001',5). It's faster as well as safer.
----
Manual Page -- http://www.php.net/manual/en/ref.bc.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40686
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40686&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40686&report=yes
Search -- http://master.php.net/manage/user-notes.php