Math_BigInteger
[ class tree: Math_BigInteger ] [ index: Math_BigInteger ] [ all elements ]

Class: Math_BigInteger

Source Location: /Math_BigInteger-1.0.2/BigInteger.php

Class Overview


Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.


Author(s):

Version:

  • 1.0.0RC4

Methods


Inherited Variables

Inherited Methods


Class Details

[line 181]
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.


[ Top ]


Method Detail

Math_BigInteger (Constructor)   [line 259]

Math_BigInteger Math_BigInteger( [optional $x = 0], [optional $base = 10])

Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.

If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using two's compliment. The sole exception to this is -10, which is treated the same as 10 is.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('0x32'16)// 50 in base-16
  5.  
  6.     echo $a->toString()// outputs 50
  7.  ?>

  • Access: public

Parameters:

optional   $x     base-10 number or base-$base number if $base set.
optional   $base     integer $base

[ Top ]

abs   [line 2625]

Math_BigInteger abs( )

Absolute value.
  • Access: public

[ Top ]

add   [line 845]

Math_BigInteger add( Math_BigInteger $y)

Adds two BigIntegers.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('10');
  5.     $b = new Math_BigInteger('20');
  6.  
  7.     $c $a->add($b);
  8.  
  9.     echo $c->toString()// outputs 30
  10.  ?>

  • Access: public

Parameters:

Math_BigInteger   $y     

[ Top ]

bitwise_and   [line 2759]

Math_BigInteger bitwise_and( Math_BigInteger $x)

Logical And
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

bitwise_leftRotate   [line 2993]

Math_BigInteger bitwise_leftRotate( Integer $shift)

Logical Left Rotate

Instead of the top x bits being dropped they're appended to the shifted bit string.

  • Access: public

Parameters:

Integer   $shift     

[ Top ]

bitwise_leftShift   [line 2956]

Math_BigInteger bitwise_leftShift( Integer $shift)

Logical Left Shift

Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.

  • Access: public

Parameters:

Integer   $shift     

[ Top ]

bitwise_not   [line 2879]

Math_BigInteger bitwise_not( )

Logical Not
  • Access: public

[ Top ]

bitwise_or   [line 2800]

Math_BigInteger bitwise_or( Math_BigInteger $x)

Logical Or
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

bitwise_rightRotate   [line 3037]

Math_BigInteger bitwise_rightRotate( Integer $shift)

Logical Right Rotate

Instead of the bottom x bits being dropped they're prepended to the shifted bit string.

  • Access: public

Parameters:

Integer   $shift     

[ Top ]

bitwise_rightShift   [line 2918]

Math_BigInteger bitwise_rightShift( Integer $shift)

Logical Right Shift

Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.

  • Access: public

Parameters:

Integer   $shift     

[ Top ]

bitwise_xor   [line 2840]

Math_BigInteger bitwise_xor( Math_BigInteger $x)

Logical Exclusive-Or
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

compare   [line 2661]

Integer compare( Math_BigInteger $y)

Compares two numbers.

Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is demonstrated thusly:

$x > $y: $x->compare($y) > 0 $x < $y: $x->compare($y) < 0 $x == $y: $x->compare($y) == 0

Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y).

  • Return: < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
  • See: Math_BigInteger::equals()
  • Access: public

Parameters:

Math_BigInteger   $y     

[ Top ]

copy   [line 739]

Math_BigInteger copy( )

Copy an object

PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee that all objects are passed by value, when appropriate. More information can be found here:

http://php.net/language.oop5.basic#51624


[ Top ]

divide   [line 1398]

Array divide( Math_BigInteger $y)

Divides two BigIntegers.

Returns an array whose first element contains the quotient and whose second element contains the "common residue". If the remainder would be positive, the "common residue" and the remainder are the same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder and the divisor (basically, the "common residue" is the first positive modulo).

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('10');
  5.     $b = new Math_BigInteger('20');
  6.  
  7.     list($quotient$remainder$a->divide($b);
  8.  
  9.     echo $quotient->toString()// outputs 0
  10.     echo "\r\n";
  11.     echo $remainder->toString()// outputs 10
  12.  ?>

  • Access: public

Parameters:

Math_BigInteger   $y     

[ Top ]

equals   [line 2719]

Boolean equals( Math_BigInteger $x)

Tests the equality of two numbers.

If you need to see if one number is greater than or less than another number, use Math_BigInteger::compare()


Parameters:

Math_BigInteger   $x     

[ Top ]

extendedGCD   [line 2482]

Math_BigInteger extendedGCD( Math_BigInteger $n)

Calculates the greatest common divisor and Bezout's identity.

Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which combination is returned is dependant upon which mode is in use. See Bezout's identity - Wikipedia for more information.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger(693);
  5.     $b = new Math_BigInteger(609);
  6.  
  7.     extract($a->extendedGCD($b));
  8.  
  9.     echo $gcd->toString("\r\n"// outputs 21
  10.     echo $a->toString($x->toString($b->toString($y->toString()// outputs 21
  11.  ?>

  • Access: public

Parameters:

Math_BigInteger   $n     

[ Top ]

gcd   [line 2613]

Math_BigInteger gcd( Math_BigInteger $n)

Calculates the greatest common divisor

Say you have 693 and 609. The GCD is 21.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger(693);
  5.     $b = new Math_BigInteger(609);
  6.  
  7.     $gcd = a->extendedGCD($b);
  8.  
  9.     echo $gcd->toString("\r\n"// outputs 21
  10.  ?>

  • Access: public

Parameters:

Math_BigInteger   $n     

[ Top ]

isPrime   [line 3296]

Boolean isPrime( [optional $t = false])

Checks a numer to see if it's prime

Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed accross multiple pageloads on a website instead of just one.

  • Access: public

Parameters:

optional   $t     Integer $t

[ Top ]

modInverse   [line 2416]

mixed modInverse( Math_BigInteger $n)

Calculates modular inverses.

Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger(30);
  5.     $b = new Math_BigInteger(17);
  6.  
  7.     $c $a->modInverse($b);
  8.     echo $c->toString()// outputs 4
  9.  
  10.     echo "\r\n";
  11.  
  12.     $d $a->multiply($c);
  13.     list($d$d->divide($b);
  14.     echo $d// outputs 1 (as per the definition of modular inverse)
  15.  ?>

  • Return: false, if no modular inverse exists, Math_BigInteger, otherwise.
  • Access: public

Parameters:

Math_BigInteger   $n     

[ Top ]

modPow   [line 1636]

Math_BigInteger modPow( Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('10');
  5.     $b = new Math_BigInteger('20');
  6.     $c = new Math_BigInteger('30');
  7.  
  8.     $c $a->modPow($b$c);
  9.  
  10.     echo $c->toString()// outputs 10
  11.  ?>

  • Access: public

Parameters:

Math_BigInteger   $e     
Math_BigInteger   $n     

[ Top ]

multiply   [line 1111]

Math_BigInteger multiply( Math_BigInteger $x)

Multiplies two BigIntegers

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('10');
  5.     $b = new Math_BigInteger('20');
  6.  
  7.     $c $a->multiply($b);
  8.  
  9.     echo $c->toString()// outputs 200
  10.  ?>

  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

powMod   [line 1776]

Math_BigInteger powMod( Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Alias for Math_BigInteger::modPow()

  • Access: public

Parameters:

Math_BigInteger   $e     
Math_BigInteger   $n     

[ Top ]

random   [line 3093]

Math_BigInteger random( [optional $min = false], [optional $max = false])

Generate a random number
  • Access: public

Parameters:

optional   $min     Integer $min
optional   $max     Integer $max

[ Top ]

randomPrime   [line 3170]

Math_BigInteger randomPrime( [optional $min = false], [optional $max = false], [optional $timeout = false])

Generate a random prime number.

If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, give up and return false.

  • Access: public

Parameters:

optional   $min     Integer $min
optional   $max     Integer $max
optional   $timeout     Integer $timeout

[ Top ]

setPrecision   [line 2738]

void setPrecision( Integer $bits)

Set Precision

Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.

  • Access: public

Parameters:

Integer   $bits     

[ Top ]

setRandomGenerator   [line 3050]

void setRandomGenerator( String $generator)

Set random number generator function

This function is deprecated.

  • Access: public

Parameters:

String   $generator     

[ Top ]

subtract   [line 976]

Math_BigInteger subtract( Math_BigInteger $y)

Subtracts two BigIntegers.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('10');
  5.     $b = new Math_BigInteger('20');
  6.  
  7.     $c $a->subtract($b);
  8.  
  9.     echo $c->toString()// outputs -10
  10.  ?>

  • Access: public

Parameters:

Math_BigInteger   $y     

[ Top ]

toBits   [line 651]

String toBits( [Boolean $twos_compliment = false])

Converts a BigInteger to a bit string (eg. base-2).

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('65');
  5.  
  6.     echo $a->toBits()// outputs '1000001'
  7.  ?>

  • Access: public

Parameters:

Boolean   $twos_compliment     

[ Top ]

toBytes   [line 527]

String toBytes( [Boolean $twos_compliment = false])

Converts a BigInteger to a byte string (eg. base-256).

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('65');
  5.  
  6.     echo $a->toBytes()// outputs chr(65)
  7.  ?>

  • Access: public

Parameters:

Boolean   $twos_compliment     

[ Top ]

toHex   [line 624]

String toHex( [Boolean $twos_compliment = false])

Converts a BigInteger to a hex string (eg. base-16)).

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('65');
  5.  
  6.     echo $a->toHex()// outputs '41'
  7.  ?>

  • Access: public

Parameters:

Boolean   $twos_compliment     

[ Top ]

toString   [line 688]

String toString( )

Converts a BigInteger to a base-10 number.

Here's an example:

  1.  <?php
  2.     include('Math/BigInteger.php');
  3.  
  4.     $a = new Math_BigInteger('50');
  5.  
  6.     echo $a->toString()// outputs 50
  7.  ?>

  • Access: public

[ Top ]

__clone   [line 776]

Math_BigInteger __clone( )

__clone() magic method

Although you can call Math_BigInteger::__toString() directly in PHP5, you cannot call Math_BigInteger::__clone() directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5, call Math_BigInteger::copy(), instead.


[ Top ]

__sleep   [line 789]

void __sleep( )

__sleep() magic method

Will be called, automatically, when serialize() is called on a Math_BigInteger object.


[ Top ]

__toString   [line 759]

void __toString( )

__toString() magic method

Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call toString().

  • Access: public

[ Top ]

__wakeup   [line 811]

void __wakeup( )

__wakeup() magic method

Will be called, automatically, when unserialize() is called on a Math_BigInteger object.


[ Top ]


Documentation generated on Wed, 12 Feb 2014 04:30:08 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.