PHP :: Bug #44995 :: bcpowmod() fails if scale != 0 |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44995 bcpowmod() fails if scale != 0
Submitted: 2008-05-14 16:26 UTC Modified: 2017-09-06 21:27 UTC
Avg. Score:4.1 ± 1.0
Reproduced:18 of 36 (50.0%)
Same Version:18 (100.0%)
Same OS:18 (100.0%)
From: nat at fishtrap dot co dot uk Assigned: cmb (profile)
Status: Closed Package: BC math related
PHP Version: 5.*, 6 (2009-09-20) OS: *
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:
Solve the problem:
45 - 29 = ?
Subscribe to this entry?

 [2008-05-14 16:26 UTC] nat at fishtrap dot co dot uk
When using the 4th optional parameter of bcpowmod. The answer is always zero and does not match the answer using bc directly.

This is also true if you set the scale using bcscale(1).

In short if the scale is non zero I can't find any way of getting anything apart from zero as the result.

Reproduce code:
echo bcpowmod("4", "4", "3", 1 ) ,PHP_EOL;

Expected result:

in bc
scale =1;

Actual result:


remove_scale_parameter_from_bcpowmod_bug44995 (last revision 2010-08-30 20:38 UTC) by

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-19 18:22 UTC]
This seems to be a bug in the bc library. I asked jhasler to look into it.
 [2009-09-20 14:43 UTC]
John Hasler said:

The example in #44995 is wrong.  "^" has higher precedence than "%" so
"4^4%3" means "(4^4)%3" or "256%3", not "4^(4%3).  The latter gives a
runtime error in bc with scale=1, as it should as fractional exponents
are not supported.

From number.c:

/* Raise BASE to the EXPO power, reduced modulo MOD.  The result is
  placed in RESULT.  If a EXPO is not an integer,
  only the integer part is used.  */
 /* Check the base for scale digits. */
 if (base->n_scale != 0)
     bc_rt_warn ("non-zero scale in base");

 /* Check the exponent for scale digits. */
 if (exponent->n_scale != 0)
     bc_rt_warn ("non-zero scale in exponent");
     bc_divide (exponent, _one_, &exponent, 0); /*truncate */

 /* Check the modulus for scale digits. */
 if (mod->n_scale != 0)
     bc_rt_warn ("non-zero scale in modulus");

As you can see, non-zero scale is not supported.  Thus the bug is in
bcpowmod().  It should not accept a scale, and the documentation should
say so.

 [2010-02-07 01:47 UTC] terrafrost at gmail dot com
Related to this...

echo bcpowmod('3234', '32345', '22345', 0) . "\r\n";
echo bcpowmod('3234', '32345', '22345', 1);

That outputs the following:


It seems to me that the output of the above ought to either be this:

Warning: bcpowmod() expects at most 3 parameters, 4 given

...or this:

 [2010-08-30 22:38 UTC]
The following patch has been added/updated:

Patch Name: remove_scale_parameter_from_bcpowmod_bug44995
Revision:   1283200734
 [2017-09-06 21:27 UTC]
-Summary: bcpowmod() should not have scale parameter (only 0 is supported) +Summary: bcpowmod() fails if scale != 0 -Assigned To: +Assigned To: cmb
 [2017-09-06 21:27 UTC]
> As you can see, non-zero scale is not supported. Thus the bug is
> in bcpowmod(). It should not accept a scale, and the
> documentation should say so.

The documentation for the scale parameter states:

| This optional parameter is used to set the number of digits
| after the decimal place in the result.

Although the fractional part of the operation is always zero,
it still might make sense to request a certain number of decimals.
So the behavior should be fixed to match the other BCMath
operations, instead of removing this parameter.

> Expected result:
> ----------------
> 0.1

The expected result would actually be 1.0.
 [2017-09-06 21:52 UTC]
Automatic comment on behalf of
Log: Fixed bug #44995 (bcpowmod() fails if scale != 0)
 [2017-09-06 21:52 UTC]
-Status: Analyzed +Status: Closed
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Thu Aug 16 19:01:24 2018 UTC