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. |
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
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.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
-
abs()
-
Make this number positive
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
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
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
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
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
Returns:
true
if this number is divisible by the test-divisor- Type
- Boolean
-
isNegative()
-
Returns:
true
if this number is negative, false if zero or positive- Type
- Boolean
-
isPositive()
-
Returns:
true
if this number is positive, false if zero or negative- Type
- Boolean
-
isZero()
-
Returns:
true
if this number is zero- Type
- Boolean
-
lsb()
-
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
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
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
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
Returns:
This number for chaining purposes
- Type
- module:N~N
-
reset()
-
Resets number to zero
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
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
Returns:
This number for chaining purposes
- Type
- module:N~N
-
toString()
-
Decimal value of this number
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 numbersinfinity
can be returned.Returns:
The value of this number accurate to within the bounds of JS native floating point number
- Type
- Number