Packageorg.granite.math
Classpublic final class BigInteger
ImplementsBigNumber

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like ActionScript3's primitive int type). BigInteger provides analogues to all of ActionScript3's primitive integer operators (+, -, *, /).

Semantics of arithmetic operations exactly mimic those of ActionScript3's integer arithmetic operators. For example, division by zero throws a BigNumberError, and division of a negative by a positive yields a negative (or zero) remainder. All of the details concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Comparison operations perform signed integer comparisons, analogous to those performed by ActionScript3's relational and equality operators.

This class is a partial ActionScript3 port of the Java class java.math.BigInteger originally written by Josh Bloch and Michael McCloskey, but relies on a completely different implementation. It provides externalization methods that are meant to be used with specific GraniteDS serialization mechanisms.

See also

Long
BigDecimal


Public Properties
 PropertyDefined by
  sign : int
[read-only] The sign of this BigInteger as an int, ie: -1, 0 or 1 as the value of this BigInteger is negative, zero or positive.
BigInteger
Public Methods
 MethodDefined by
  
BigInteger(value:* = null, radix:int = 10)
Constructs a new BigInteger instance according to the supplied parameters.
BigInteger
  
Returns a BigInteger whose value is the absolute value of this BigInteger.
BigInteger
  
Returns a BigInteger whose value is (this + b).
BigInteger
  
BigInteger
  
compareTo(b:BigInteger, unsigned:Boolean = false):int
Compares this BigInteger with the specified BigInteger.
BigInteger
  
Returns a BigInteger whose value is (this / b).
BigInteger
  
divideAndRemainder(b:*):Array
Returns an array of two BigInteger containing (this / b) followed by (this % b).
BigInteger
  
equals(b:*):Boolean
Compares this BigInteger with the specified object for equality.
BigInteger
  
Returns the maximum of this BigInteger and b.
BigInteger
  
Returns the minimum of this BigInteger and b.
BigInteger
  
Returns a BigInteger whose value is (this * b).
BigInteger
  
Returns a BigInteger whose value is (-this).
BigInteger
  
Returns a BigInteger whose value is (this % b).
BigInteger
  
Returns a BigInteger whose value is (this - b).
BigInteger
  
toInt():int
Converts this BigInteger to an int: if this BigInteger is too big to fit in an int, only the low-order 31 bits are returned and the sign of the result is preserved as it was in the orginal BigInteger.
BigInteger
  
toNumber():Number
Converts this BigInteger to a Number.
BigInteger
  
toString(radix:int = 10, abs:Boolean = false):String
Returns the String representation of this BigInteger in the given radix.
BigInteger
Public Constants
 ConstantDefined by
  ONE : BigInteger
[static] The BigInteger constant one.
BigInteger
  TEN : BigInteger
[static] The BigInteger constant ten.
BigInteger
  ZERO : BigInteger
[static] The BigInteger constant zero.
BigInteger
Property detail
signproperty
sign:int  [read-only]

The sign of this BigInteger as an int, ie: -1, 0 or 1 as the value of this BigInteger is negative, zero or positive.

Implementation
    public function get sign():int
Constructor detail
BigInteger()constructor
public function BigInteger(value:* = null, radix:int = 10)

Constructs a new BigInteger instance according to the supplied parameters.

The value parameter may be a String representation of an integer, a int primitive value, a Number or even another BigInteger:

Parameters
value:* (default = null) — the value to be assigned to the new BigInteger.
 
radix:int (default = 10) — the radix (2 <= radix <= 36) to be used for string conversion (ignored if the value parameter isn't a string).

Throws
NumberFormatError — if the value parameter is an invalid String representation.
 
IllegalArgumentError — if the value parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.
Method detail
abs()method
public function abs():BigInteger

Returns a BigInteger whose value is the absolute value of this BigInteger.

Returns
BigInteger — the absolute value of this BigInteger.
add()method 
public function add(b:*):BigInteger

Returns a BigInteger whose value is (this + b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value to be added to this BigInteger.

Returns
BigInteger(this + b).

Throws
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

clone()method 
public function clone():BigInteger

Returns
BigInteger
compareTo()method 
public function compareTo(b:BigInteger, unsigned:Boolean = false):int

Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, <=, !=, >=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.

Parameters
b:BigInteger — the BigInteger to which this BigInteger is to be compared.
 
unsigned:Boolean (default = false) — if true, the comparison is made between absolute values (ie: this.abs().compareTo(b.abs())).

Returns
int — -1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than b.
divide()method 
public function divide(b:*):BigInteger

Returns a BigInteger whose value is (this / b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value by which this BigInteger is to be divided.

Returns
BigInteger(this / b).

Throws
ArithmeticError — if the b parameter is equals to 0.
 
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

divideAndRemainder()method 
public function divideAndRemainder(b:*):Array

Returns an array of two BigInteger containing (this / b) followed by (this % b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value by which this BigInteger is to be divided, and the remainder computed.

Returns
Array — an array of two BigIntegers: the quotient (this / val) is the initial element, and the remainder (this % val) is the final element.

Throws
ArithmeticError — if the b parameter is equals to 0.
 
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

equals()method 
public function equals(b:*):Boolean

Compares this BigInteger with the specified object for equality.

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — an object to which this BigInteger is to be compared.

Returns
Booleantrue if and only if the specified object is a BigInteger (or convertible to a BigInteger) whose value is numerically equal to this BigInteger.

See also

max()method 
public function max(b:*):BigInteger

Returns the maximum of this BigInteger and b.

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — the value with which the maximum is to be computed.

Returns
BigInteger — the BigInteger whose value is the greater of this BigInteger and b. If they are equal, either may be returned.

Throws
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

min()method 
public function min(b:*):BigInteger

Returns the minimum of this BigInteger and b.

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — the value with which the minimum is to be computed.

Returns
BigInteger — the BigInteger whose value is the lesser of this BigInteger and b. If they are equal, either may be returned.

Throws
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

multiply()method 
public function multiply(b:*):BigInteger

Returns a BigInteger whose value is (this * b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value to be multiplied by this BigInteger.

Returns
BigInteger(this * b).

Throws
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

negate()method 
public function negate():BigInteger

Returns a BigInteger whose value is (-this).

Returns
BigInteger(-this).
remainder()method 
public function remainder(b:*):BigInteger

Returns a BigInteger whose value is (this % b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value by which this BigInteger is to be divided, and the remainder computed.

Returns
BigInteger(this % b).

Throws
ArithmeticError — if the b parameter is equals to 0.
 
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

subtract()method 
public function subtract(b:*):BigInteger

Returns a BigInteger whose value is (this - b).

The b parameter may be of any of the supported types as specified in the BigInteger constructor documentation (a radix of 10 is assumed for String representations).

Parameters
b:* — b the value to be subtracted from this BigInteger.

Returns
BigInteger(this - b).

Throws
NumberFormatError — if the b parameter is an invalid String representation (for radix 10).
 
IllegalArgumentError — if the b parameter is not one of the supported types or if it is Number.NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY.

See also

toInt()method 
public function toInt():int

Converts this BigInteger to an int: if this BigInteger is too big to fit in an int, only the low-order 31 bits are returned and the sign of the result is preserved as it was in the orginal BigInteger.

Note this conversion is slightly different than the Java one: it always preserves the sign and conforms to following rules:

Returns
int — this BigInteger converted to an int.
toNumber()method 
public function toNumber():Number

Converts this BigInteger to a Number. This conversion is done by converting this BigInteger to signed String representation and constructing a new Number from this representation: if this BigInteger has atoo great magnitude to be represented as a Number, it will be converted to Number.NEGATIVE_INFINITY or Number.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

Returns
Number — this BigInteger converted to a Number.
toString()method 
public function toString(radix:int = 10, abs:Boolean = false):String

Returns the String representation of this BigInteger in the given radix. If the radix is outside the range from 2 to 36 inclusive, an error is thrown. The digit-to-character mapping uses "0...9, a...z" characters, and a minus sign is prepended if appropriate.

Parameters
radix:int (default = 10) — the radix of the String representation.
 
abs:Boolean (default = false) — if true, the minus sign is skipped even if this BigInteger is negative.

Returns
String — String representation of this BigInteger in the given radix.

See also

Constant detail
ONEconstant
public static const ONE:BigInteger

The BigInteger constant one.

TENconstant 
public static const TEN:BigInteger

The BigInteger constant ten.

ZEROconstant 
public static const ZERO:BigInteger

The BigInteger constant zero.