Raise an arbitrary precision number to another, reduced by a specified modulus (original) (raw)

bcpowmod

(PHP 5, PHP 7)

bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus

Description

bcpowmod ( string $base , string $exponent , string $modulus [, int $scale = 0 ] ) : string

Parameters

base

The base, as an integral string (i.e. the scale has to be zero).

exponent

The exponent, as an non-negative, integral string (i.e. the scale has to be zero).

modulus

The modulus, as an integral string (i.e. the scale has to be zero).

scale

This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set.

Return Values

Returns the result as a string, or FALSE if modulus is 0 or exponent is negative.

Notes

Note:

Because this method uses the modulus operation, numbers which are not positive integers may give unexpected results.

Examples

The following two statements are functionally identical. Thebcpowmod() version however, executes in less time and can accept larger parameters.

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mo>=</mo><mi>b</mi><mi>c</mi><mi>p</mi><mi>o</mi><mi>w</mi><mi>m</mi><mi>o</mi><mi>d</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">a = bcpowmod(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">c</span><span class="mord mathnormal">p</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">m</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mopen">(</span></span></span></span>x, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">y, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mpunct">,</span></span></span></span>mod);$b = bcmod(bcpow($x, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">y), </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mpunct">,</span></span></span></span>mod);// <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>a</mi><mi>n</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">a and </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">aan</span><span class="mord mathnormal">d</span></span></span></span>b are equal to each other.?>

See Also