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

Class: Math_BigInteger

Source Location: /Math_BigInteger-1.0.0RC2/Math_BigInteger.php

Class Overview


Pure-PHP arbitrary precission integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers. Negative numbers are supported in all publically accessable functions save for modPow and modInverse.


Author(s):

Version:

  • 1.0.0RC2

Methods


Inherited Variables

Inherited Methods


Class Details

[line 160]
Pure-PHP arbitrary precission integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers. Negative numbers are supported in all publically accessable functions save for modPow and modInverse.


[ Top ]


Method Detail

Math_BigInteger (Constructor)   [line 196]

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

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

Here's a quick 'n dirty example:

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

  • Access: public

Parameters:

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

[ Top ]

abs   [line 1532]

Math_BigInteger abs( )

Absolute value.
  • Access: public

[ Top ]

add   [line 486]

Math_BigInteger add( Math_BigInteger $y)

Adds two BigIntegers.

Here's a quick 'n dirty 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 1616]

Math_BigInteger bitwise_and( Math_BigInteger $x)

Logical And
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

bitwise_not   [line 1722]

Math_BigInteger bitwise_not( [$bits $bits = -1])

Logical Not

Although integers can be converted to and from various bases with relative ease, there is one piece of information that is lost during such conversions. The number of leading zeros that number had or should have in any given base. Per that, if you convert 1 from decimal to binary, there's no way to know just how many leading zero's there should be. In truth, there could be any number.

Normally, the number of leading zero's is unimportant. When doing "not", however, it is. The "not" of 1 on an 8-bit representation of 1 is 1111 1110. The "not" of 1 on a 16-bit representation of 1 is 1111 1111 1111 1110. When doing it on a number that's preceeded by an infinite number of zero's, it's infinite.

This function assumes that there are no leading zero's - that the bit-representation being used is equal to the minimum number of required bits, unless otherwise specified in the optional parameter, where the optional parameter represents the bit-representation being used. If the specified bit-representation is smaller than the minimum number of bits required to represent the number, the latter will be used as the bit-representation.

  • Access: public

Parameters:

$bits   $bits     Integer

[ Top ]

bitwise_or   [line 1646]

Math_BigInteger bitwise_or( Math_BigInteger $x)

Logical Or
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

bitwise_xor   [line 1676]

Math_BigInteger bitwise_xor( Math_BigInteger $x)

Logical Exclusive-Or
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

compare   [line 1558]

Integer compare( Math_BigInteger $x)

Compares two numbers.
  • Return: < 0 if $this is less than $x; > 0 if $this is greater than $x, and 0 if they are equal.
  • Access: public

Parameters:

Math_BigInteger   $x     

[ Top ]

divide   [line 812]

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.

Here's a quick 'n dirty 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 ]

modInverse   [line 1384]

mixed modInverse( Math_BigInteger $n)

Calculates modular inverses.

Here's a quick 'n dirty 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.  
  9.     echo $c->toString()// outputs 4
  10.   ?>

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

Parameters:

Math_BigInteger   $n     

[ Top ]

modPow   [line 995]

Math_BigInteger modPow( Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Here's a quick 'n dirty 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 673]

Math_BigInteger multiply( Math_BigInteger $x)

Multiplies two BigIntegers

Here's a quick 'n dirty 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 ]

subtract   [line 573]

Math_BigInteger subtract( Math_BigInteger $y)

Subtracts two BigIntegers.

Here's a quick 'n dirty 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 ]

toBytes   [line 367]

String toBytes( )

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

Here's a quick 'n dirty 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

[ Top ]

toString   [line 432]

String toString( )

Converts a BigInteger to a base-10 number.

Here's a quick 'n dirty 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 ]


Documentation generated on Thu, 16 Nov 2006 21:00:09 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.