# bug on bc math?

- Thread: https://externals.io/message/61873
- Original archive: https://news-web.php.net/php.internals/61873
- Started by Devis Lucato on 2012-07-30T12:47:30+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61873 — Devis Lucato — 2012-07-30T12:47:30+00:00

URL: https://externals.io/message/61873#61873

Hi,

before raising a bug I was wondering if anyone noticed this odd behavior in
BC math functions ?

Actual

php -r "echo bcmul('1.1', '9.0', 5);" ==> 9.90
php -r "bcscale(5); echo bcmul('1.1', '9.0');" ==> 9.90
php -r "bcscale(5); echo bcadd('8.9', '1.0');" ==> 9.90000


Expected

php -r "echo bcmul('1.1', '9.0', 5);" ==> 9.90000
php -r "bcscale(5); echo bcmul('1.1', '9.0');" ==> 9.90000
php -r "bcscale(5); echo bcadd('8.9', '1.0');" ==> 9.90000


Seen on 5.3 and 5.4.

Looking at
http://git.php.net/?p=php-src.git;a=history;f=ext/bcmath/bcmath.c;h=5dfafa8bd8dbc1703af2ea4452ac2b05b765a619;hb=HEADI
couldn't see any recent change related to this bug.

Thank you

---

## #61875 — Laruence — 2012-07-30T13:52:29+00:00

URL: https://externals.io/message/61873#61875 · In reply to: #61873

Hi:

I'd like mark this as a doc problem,  see the comment of the bcmatch lib:

/* The multiply routine.  N2 times N1 is put int PROD with the scale of
   the result being MIN(N2 scale+N1 scale, MAX (SCALE, N2 scale, N1 scale)).
   */
    void  bc_multiply (bc_num n1, bc_num n2, bc_num *prod, int scale TSRMLS_DC)

and

* Here is the full add routine that takes care of negative numbers.
   N1 is added to N2 and the result placed into RESULT.  SCALE_MIN
   is the minimum scale for the result. */

void  bc_add (n1, n2, result, scale_min)

thanks


> Hi,
>
> before raising a bug I was wondering if anyone noticed this odd behavior in
> BC math functions ?
>
> Actual
>
> php -r "echo bcmul('1.1', '9.0', 5);" ==> 9.90
> php -r "bcscale(5); echo bcmul('1.1', '9.0');" ==> 9.90
> php -r "bcscale(5); echo bcadd('8.9', '1.0');" ==> 9.90000
>
>
> Expected
>
> php -r "echo bcmul('1.1', '9.0', 5);" ==> 9.90000
> php -r "bcscale(5); echo bcmul('1.1', '9.0');" ==> 9.90000
> php -r "bcscale(5); echo bcadd('8.9', '1.0');" ==> 9.90000
>
>
> Seen on 5.3 and 5.4.
>
> Looking at
> http://git.php.net/?p=php-src.git;a=history;f=ext/bcmath/bcmath.c;h=5dfafa8bd8dbc1703af2ea4452ac2b05b765a619;hb=HEADI
> couldn't see any recent change related to this bug.
>
> Thank you



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #61876 — Laruence — 2012-07-30T14:11:41+00:00

URL: https://externals.io/message/61873#61876 · In reply to: #61875

> Hi:
>
> I'd like mark this as a doc problem,  see the comment of the bcmatch lib:
>
> /* The multiply routine.  N2 times N1 is put int PROD with the scale of
>    the result being MIN(N2 scale+N1 scale, MAX (SCALE, N2 scale, N1 scale)).
>    */
>     void  bc_multiply (bc_num n1, bc_num n2, bc_num *prod, int scale TSRMLS_DC)
>
> and
>
> * Here is the full add routine that takes care of negative numbers.
>    N1 is added to N2 and the result placed into RESULT.  SCALE_MIN
>    is the minimum scale for the result. */
>
> void  bc_add (n1, n2, result, scale_min)
>
> thanks
Hi:
   but on the hand, we may also consider this as a bug,  behavior not
act like the doc said :)

   any IMO,  fixing this, we should add some logic in PHP side,  like
before returning value to user .

   the code will look like(for bc_multiply):


   if (result->n_scale < scale ) {
       result->n_value =  erealloc($result->n_value, scale);

       memset(result->n_result + result->n_scale, '0', scale - result->n_scale);

       result->n_scale = scale;
   }

   this will be a little ugly, since it alter the bc_num struct which
should only inside bc lib,   what do you think?

thanks
>
> 
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #61878 — Stas Malyshev — 2012-07-30T17:12:27+00:00

URL: https://externals.io/message/61873#61878 · In reply to: #61876

Hi!

>    but on the hand, we may also consider this as a bug,  behavior not
> act like the doc said :)

If the library we are wrapping behaves in certain way, our function
should behave in the same way. If docs say otherwise, docs should be fixed.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
