php_str2num(&second, ZSTR_VAL(right));
php_str2num(&mod, ZSTR_VAL(modulus));
- if (bc_raisemod(first, second, mod, &result, scale) != -1) {
- RETVAL_STR(bc_num2str_ex(result, scale));
- } else {
- RETVAL_FALSE;
+ switch (bc_raisemod(first, second, mod, &result, scale)) {
+ case 0:
+ RETVAL_STR(bc_num2str_ex(result, scale));
+ break;
+ case -1:
+ zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
+ break;
+ case -2:
+ zend_argument_value_error(2, "must be greater than 0");
+ break;
}
bc_free_num(&first);
function bcmod(string $dividend, string $divisor, ?int $scale = null): string {}
-function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {}
+function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string {}
function bcpow(string $base, string $exponent, ?int $scale = null): string {}
#define arginfo_bcmod arginfo_bcdiv
-ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpowmod, 0, 3, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0)
/* Check for correct numbers. */
if (bc_is_zero(mod)) return -1;
- if (bc_is_neg(expo)) return -1;
+ if (bc_is_neg(expo)) return -2;
/* Set initial values. */
power = bc_copy_num (base);
--- /dev/null
+--TEST--
+bc_raisemod's expo can't be negative
+--CREDITS--
+Gabriel Caruso (carusogabriel34@gmail.com)
+--SKIPIF--
+<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
+--FILE--
+<?php
+try {
+ var_dump(bcpowmod('1', '-1', '2'));
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+?>
+--EXPECT--
+bcpowmod(): Argument #2 ($exponent) must be greater than 0
--TEST--
-bc_raisemod's mod can't be zero and expo can't be negative
+bc_raisemod's mod can't be zero
--CREDITS--
Gabriel Caruso (carusogabriel34@gmail.com)
--SKIPIF--
<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
--FILE--
-<?php var_dump(bcpowmod('1', '-1', '0')); ?>
+<?php
+try {
+ var_dump(bcpowmod('1', '-1', '0'));
+} catch (DivisionByZeroError $ex) {
+ echo $ex->getMessage(), PHP_EOL;
+}
+?>
--EXPECT--
-bool(false)
+Modulo by zero
F1("bcmul", MAY_BE_STRING),
F1("bcdiv", MAY_BE_STRING),
F1("bcmod", MAY_BE_STRING),
- F1("bcpowmod", MAY_BE_FALSE | MAY_BE_STRING),
+ F1("bcpowmod", MAY_BE_STRING),
F1("bcpow", MAY_BE_STRING),
F1("bcsqrt", MAY_BE_STRING),