Bug #44995 [Ana->Csd]: bcpowmod() fails if scale != 0
Edit report at https://bugs.php.net/bug.php?id=44995&edit=1
ID: 44995
Updated by: cmb@php.net
Reported by: nat at fishtrap dot co dot uk
Summary: bcpowmod() fails if scale != 0
-Status: Analyzed
+Status: Closed
Type: Bug
Package: BC math related
Operating System: *
PHP Version: 5.*, 6 (2009-09-20)
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dea41f3c3a003a22893b27eead429f963d3230c3
Log: Fixed bug #44995 (bcpowmod() fails if scale != 0)
Previous Comments:
------------------------------------------------------------------------
[2017-09-06 21:27:16] cmb@php.net
> 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.
------------------------------------------------------------------------
[2010-08-30 22:38:54] k.schroeder@php.net
The following patch has been added/updated:
Patch Name: remove_scale_parameter_from_bcpowmod_bug44995
Revision: 1283200734
URL: http://bugs.php.net/patch-display.php?bug=44995&patch=remove_scale_parameter_from_bcpowmod_bug44995&revision=1283200734
------------------------------------------------------------------------
[2010-02-07 01:47:52] terrafrost at gmail dot com
Related to this...
<?php
echo bcpowmod('3234', '32345', '22345', 0) . "\r\n";
echo bcpowmod('3234', '32345', '22345', 1);
?>
That outputs the following:
17334
1140.8
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:
17334
17334.0
------------------------------------------------------------------------
[2009-09-20 14:43:23] sjoerd@php.net
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.
------------------------------------------------------------------------
[2009-09-19 18:22:41] sjoerd@php.net
This seems to be a bug in the bc library. I asked jhasler to look into it.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=44995
--
Edit this bug report at https://bugs.php.net/bug.php?id=44995&edit=1
Thread (9 messages)