Class: N

N~ N


new N(initialValue)

N is the core vehicle to store and manipulate arbitrarily long integers

This class is not intended for external use and is only used internally by the TPA class. It does not form any part of the TPA API. It is documented here for developer convenience and interest. The minified browser build gives no access to this class except except via the internals of Tpa objects

For efficiency reasons many methods do not have error checking or may make assumptions about the state of the number.
Any assumptions, however, are documented.

Parameters:
Name Type Argument Description
initialValue Number | String | module:N~N <optional>

The initial value assigned to this number.
This may be a JS numeric value, a string or another N instance. If numeric, any fractional part is ignored.
If string it may only contain digits starting with an optional + or - sign. If N, the new N will be a duplicate.

Source:
Throws:

If parameter passed is not one of the above or is badly formed.

Type
external:Error

Methods


<static> abs(number)

Create a positive version of the number given

Parameters:
Name Type Description
number module:N~N

The number to copy and make positive

Source:
Returns:

A new number that is the positive version of number

Type
module:N~N

<static> getBASE()

The BASE to which all numbers are stored.

This is preset to 2^25 which is the optimal size.
However, it may be lower (not higher!) in order to test the integrity of internal representation and manipulation of numbers.
In the browser version this method is not available.

Source:
Returns:
Type
Number

<static> setBASE(base)

Sets the BASE to which numbers are internally represented.

This should only be set for experimentation and testing purposes.
Any existing instances of Tpa will become corrupt or invalid once the base is changed.
In the browser version this method is not available.
Safe rule: Don't call this set method!

Parameters:
Name Type Description
base Number

The new BASE for number internal representation

Source:

abs()

Make this number positive

Source:
Returns:

This number for chaining purposes

Type
module:N~N

add(number)

Addition

Parameters:
Name Type Description
number module:N~N

The number to add to this number

Source:
Returns:

This number for chaining purposes

Type
module:N~N

compare(comparison)

Compares two numbers

Note This function can only be called when both numbers are positivised (and hence normalised)

Parameters:
Name Type Description
comparison module:N~N

The number to compare with this number

Source:
See:
Returns:

0 if equal, -1 if this < comparison, +1 if this > comparison

Type
number

digitDivide(digit)

Division by a single digit

The number provided must be less than BASE

Parameters:
Name Type Description
digit Number

The number to multiply with

Source:
Returns:

The remainder

Type
Number

digitMultiply(digit)

Multiplication by a single digit

The number provided must be less than BASE

Parameters:
Name Type Description
digit Number

The number to multiply with

Source:
Returns:

This number for chaining purposes

Type
module:N~N

divide(number)

Division

Parameters:
Name Type Description
number module:N~N

The number to divide into this number

Source:
Throws:

If divisor is zero

Type
Error
Returns:

The remainder. Note: This number is not returned, unlike most other mutation operations

Type
module:N~N

isDivisibleBy(testDivisor)

Checks for divisibility.

The divisor must be positive and less than the BASE

Parameters:
Name Type Description
testDivisor Number

The number to test as a factor of this number

Source:
Returns:

true if this number is divisible by the test-divisor

Type
Boolean

isNegative()

Source:
Returns:

true if this number is negative, false if zero or positive

Type
Boolean

isPositive()

Source:
Returns:

true if this number is positive, false if zero or negative

Type
Boolean

isZero()

Source:
Returns:

true if this number is zero

Type
Boolean

lsb()

Source:
Returns:

The least significant digit of the number or 0 if zero

Type
Number

multiply(number)

Multiplication

Parameters:
Name Type Description
number module:N~N

The number to multiply this number

Source:
Returns:

This number for chaining purposes

Type
module:N~N

negate()

Make this number the opposite sign

If the number is positive it will be made negative, if negative it will be made positive

Source:
Returns:

This number for chaining purposes

Type
module:N~N

normalise(noReduction)

Ensures that all digits of this number are less than the BASE

Internal representation allows for digits to exceed BASE. Normalisation
is necessary where an impending operation depends on this not being the case.
Note also that normalisation does not remove negative digits that are
also permitted. Again, for some operations, negative digits are undesired
and the positivise method is required to remove them.

Parameters:
Name Type Argument Default Description
noReduction Boolean <optional>
false

set to true if we wish to keep any trailing zero digits

Source:
See:
Returns:

This number for chaining purposes

Type
module:N~N

positivise()

Removes any negative digits by carry over.

This function should only be called once it is established that the number is both positive overall
and is normalised.

Source:
See:

quotient(number)

The quotient of this number after dividing it by the number given

Parameters:
Name Type Description
number module:N~N

The divisor

Source:
Returns:

This number for chaining purposes

Type
module:N~N

random(digits)

Sets a number to a random value that is roughly the number of decimal digits given

Parameters:
Name Type Description
digits Number

The number of decimal digits required

Source:
Returns:

This number for chaining purposes

Type
module:N~N

reset()

Resets number to zero

Source:
Returns:

This number for chaining purposes

Type
module:N~N

set(newValue)

Sets a new numeric value into this number

Parameters:
Name Type Description
newValue Number

As per instantiation, setting Tpa with a JS native number is subject to accuracy constraints

Source:
Returns:

This number for chaining purposes

Type
module:N~N

subtract(number)

Subtraction

Parameters:
Name Type Description
number module:N~N

The number to subtract from this number

Source:
Returns:

This number for chaining purposes

Type
module:N~N

toString()

Decimal value of this number

Source:
Returns:

The full decimal representation of this number

Type
String

value()

Gets the numeric value of this number

The JS native number returned is subject to accuracy constraints and thus this method is only useful
as an indicator. Indeed, for very large numbers infinity can be returned.

Source:
Returns:

The value of this number accurate to within the bounds of JS native floating point number

Type
Number