Apache Zeta Components Manual :: File Source for bcmath_library.php
Source for file bcmath_library.php
Documentation is available at bcmath_library.php
* File containing the ezcAuthenticationBcmathLibrary class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @package Authentication
* Wrapper class for the PHP bcmath extension.
* @package Authentication
class ezcAuthenticationBcmathLibrary extends ezcAuthenticationBignumLibrary
* Creates a new big number from $number in the base $base.
* In the PHP extension bcmath the numbers are used as strings, not as
* strings, so this function returns the provided $number as it is, and
* without transforming it in the base $base.
* @param string $number The number from which to create the result
* @param int $base The base in which the result will be
public function init( $number, $base =
10 )
* @param string $a The first number
* @param string $b The second number
public function add( $a, $b )
* Substracts two numbers.
* @param string $a The first number
* @param string $b The second number
public function sub( $a, $b )
* Multiplies two numbers.
* @param string $a The first number
* @param string $b The second number
public function mul( $a, $b )
* @param string $a The first number
* @param string $b The second number
public function div( $a, $b )
return bcdiv( $a, $b, 0 );
* Computes $base modulo $modulus.
* @param string $base The number to apply modulo to
* @param string $modulus The modulo value to be applied to $base
public function mod( $base, $modulus )
return bcmod( $base, $modulus );
* Computes $base to the power of $exponent.
* @param string $base The number to be exponentiated
* @param string $exponent The exponent to apply to $base
public function pow( $base, $exponent )
return bcpow( $base, $exponent );
* Computes $base to the power of $exponent and then applies modulo $modulus.
* @param string $base The number to be exponentiated
* @param string $exponent The exponent to apply to $base
* @param string $modulus The modulo value to be applied to the result
public function powmod( $base, $exponent, $modulus )
return bcpowmod( $base, $exponent, $modulus );
* Computes the inverse of $number in modulo $modulus.
* @param string $number The number for which to calculate the inverse
* @param string $modulus The modulo value in which the inverse is calculated
public function invert( $number, $modulus )
while ( bccomp( $number, 0 ) <
0 )
$number =
bcadd( $number, $modulus );
$r =
$this->gcd( $number, $modulus );
while ( bccomp( $a, 0 ) <
0 )
$a =
bcadd( $a, $modulus );
* Finds the greatest common denominator of two numbers using the extended
* The returned array is ( x, y, gcd( a, b ) ), where
* x * a + y * b = gcd( a, b )
* @param string $a The first number
* @param string $b The second number
public function gcd( $a, $b )
while ( bccomp( $b, 0 ) !==
0 )
$x =
bcsub( $xLast, bcmul( $q, $x ) );
$y =
bcsub( $yLast, bcmul( $q, $y ) );
return array( $xLast, $yLast, $a );
* - a positive value if $a > $b
* - a negative value if $a < $b
* @param string $a The first number
* @param string $b The second number
public function cmp( $a, $b )
* Returns the string representation of number $a.
* @param string $number The number to be represented as a string
public function toString( $number )