Class: Tpa

TPA~ Tpa


new Tpa(initialValue, isInteger)

Tpa stores and manipulates rational numbers with total precision

Parameters:
Name Type Argument Default Description
initialValue number | string | module:TPA~Tpa <optional>

Initial value to set this number.

  1. Numeric values are only represented to a precision of 8 decimal places and, in any case, are limited by the precision of a JS floating point number. To initialise a number with definite accuracy the string form is recommended.
  2. String values can be represented in decimal or fractional form.
    • Decimal form: <+/->iii.ddd[rrr] where +/- is optional, iii represents the integer part and ddd the decimal part with [rrr] representing an optional recurring decimal part
    • Fractional form: <+/-> iii nnn/ddd where +/- is optional, iii represents the (optional) integer part, nnn the numerator and ddd the denominator. The fraction may be top heavy.
  3. Tpa instance causes this constructor to return a copy of it.

Tpa may be called statically, in which case a new instance is still returned.
Note well: If initialValue is itself a Tpa, then the same Tpa is returned without making a copy when called statically.

isInteger boolean <optional>
true

Set to false to enable this number to represent fractions.
If the initialValue is fractional in any way then isInteger will default to false.
The initial setting of this number (integer or fractional) is always kept throughout its life unless the makeFractional or makeInteger methods are called to change it.

Source:
See:
Throws:

If the constructor parameters are not valid

Type
external:Error
Example
var a=new Tpa();                 // Creates a new number set to zero
var b=Tpa(20);                   // Creates a new number preset to 20
var c=Tpa['0.[6]'];              // Creates a new number preset to 2/3
var d=new Tpa('2/3'];            // Creates a new number preset to 2/3
var e=new Tpa('-4 538/1284');    // Creates a new number preset to -4.4[19003115264797507788161993769470404984423676012461059]
var f=new Tpa(b);                // Creates a new number preset to 20
var g=Tpa(e);                    // Does NOT create a new number: Object g references Object f
var h=Tpa(false);                // Creates a new number set to zero but is configured to represent fractions
var i=Tpa(100,false);            // Creates a new number set to 100 but is configured to represent fractions
var j=Tpa(100.5,true);           // Creates a new number set to 100 as we explicitly set it to be integer only and the fractional part is ignored
var k=Tpa('10 20/3',true);       // Creates a new number set to 16 as we explicitly set it to be integer only and the fractional part is ignored

Methods


<static> abs(n)

Absolute value of a number

Parameters:
Name Type Description
n number | string | module:TPA~Tpa

The number

Source:
Returns:

|n|

Type
module:TPA~Tpa

<static> add(a, b)

Adds two numbers

Aliases: plus

Parameters:
Name Type Description
a number | string | module:TPA~Tpa

First number

b number | string | module:TPA~Tpa

Second number

Source:
Returns:

a + b

Type
module:TPA~Tpa

<static> divide(a, b)

Divides two numbers ( a / b )

Aliases: div

Parameters:
Name Type Description
a number | string | module:TPA~Tpa

First number

b number | string | module:TPA~Tpa

Second number

Source:
Returns:

a / b

Type
module:TPA~Tpa

<static> frac(number)

Sets the integer part of a number to zero

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number from which the integer part is to be removed

Source:

<static> int(number)

Sets the fractional part of a number to zero

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number from which the fractional part is to be removed

Source:

<static> makeFractional(number)

Sets a number to hold fractional value

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to copy

Source:
Returns:

A new number with the ability to hold fractions

Type
module:TPA~Tpa

<static> makeInteger(number)

Sets a number to hold integer values only

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to copy

Source:
Returns:

A new number without the ability to hold fractions

Type
module:TPA~Tpa

<static> modulus(a, b)

Modulus of two numbers

Aliases: mod

Parameters:
Name Type Description
a number | string | module:TPA~Tpa

First number

b number | string | module:TPA~Tpa

Second number

Source:
Returns:

a mod b

Type
module:TPA~Tpa

<static> multiply(a, b)

Multiplies two numbers

Aliases: mult, times

Parameters:
Name Type Description
a number | string | module:TPA~Tpa

First number

b number | string | module:TPA~Tpa

Second number

Source:
Returns:

a * b

Type
module:TPA~Tpa

<static> random(digits)

Creates a random number of an approximate number of decimal digits long

Parameters:
Name Type Description
digits number

The number of decimal digits

Source:
Returns:

A new number set a a random value

Type
module:TPA~Tpa

<static> subtract(a, b)

Subtracts two numbers

Aliases: minus, sub

Parameters:
Name Type Description
a number | string | module:TPA~Tpa

First number

b number | string | module:TPA~Tpa

Second number

Source:
Returns:

a - b

Type
module:TPA~Tpa

abs()

Takes the absolute value of this number

Source:
Returns:

This number for chaining purposes

Type
number | string | module:TPA~Tpa

add(number)

Adds the given number to this number

Aliases: plus

If this number is fractional, then it will perform a full fractional addition.
If it is set as an integer then the addition will ignore any fractional part of the number to be added

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to add

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

compare(number)

Compares the given number with this number

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

-1 if this number is less than the given number, 0 if equal, 1 if greater

Type
number

divide(number)

Divide this number by the one given

Aliases: div

If this number is fractional, then it will perform a full fractional division.
If it is set as an integer then the division will ignore any fractional part of the divisor

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to multiply by

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

eq(number)

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

true if this number is equal to the given number

Type
boolean

frac()

Sets the integer part of this number to zero

Source:
Returns:

This number for chaining purposes

Type
number | string | module:TPA~Tpa

gt(number)

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

true if this number is greater than the given number

Type
boolean

gte(number)

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

true if this number is greater than or equal to the given number

Type
boolean

hasFraction()

Source:
Returns:

true if this number has a non-zero fractional part

Type
boolean

int()

Sets the fractional part of this number to zero

Source:
Returns:

This number for chaining purposes

Type
number | string | module:TPA~Tpa

isFractional()

Source:
Returns:

true if this number is capable of representing fractions

Type
boolean

isInteger()

Source:
Returns:

true if this number only represents integers

Type
boolean

isNegative()

Source:
Returns:

true if this number is less than zero

Type
boolean

isPositive()

Source:
Returns:

true if this number is greater than zero

Type
boolean

isZero()

Source:
Returns:

true if this number is equal to zero

Type
boolean

lt(number)

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

true if this number is less than the given number

Type
boolean

lte(number)

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to compare

Source:
Returns:

true if this number is less than or equal to the given number

Type
boolean

makeFractional()

Sets this number to accept fractional amounts, if not already set

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

makeInteger()

Sets this number to hold integers only - removes any existing fractional part

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

modulus(number)

Sets this number to the modulus of the number given

Aliases: mod

Fractional parts of either number are ignored - the modulus is based on the integer parts ony

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The divisor number

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

multiply(number)

Multiply this number by the one given

Aliases: mult

If this number is fractional, then it will perform a full fractional multiplication.
If it is set as an integer then the multiplication will ignore any fractional part of the multiplier

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to multiply by

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

set(initialValue, isInteger)

Sets this number to a new value

Parameters passed are exactly those expected for construction of a new Tpa

Parameters:
Name Type Argument Default Description
initialValue number | string | module:TPA~Tpa <optional>

Initial value to set this number.

isInteger boolean <optional>
true

Set to false to enable this number to represent fractions.

Source:
Returns:

this number for chaining purposes

Type
module:TPA~Tpa

sign()

Source:
Returns:

-1 if this number is negative, 0 if zero or 1 if positive

Type
number

simplify(milliseconds)

Attempts a simplification of the remaining fraction

Finding common factors (which would be prime numbers) is a time-consuming job.
Just as well, as otherwise most security mechanism (i.e. RSA) could be hacked in a jiffy.
So there is a limit to how large a fraction can be simplified. A realistic limit has therefore been
established here whereby prime factors can not exceed the BASE of the internal number representation.
Thus the highest prime explored is 33,554,393.
Fractions that have their numerator larger than the square of this number may not be completely simplified - i.e. numbers of more than 15 digits.

Parameters:
Name Type Argument Default Description
milliseconds number <optional>
100

The maximum time in milliseconds to attempt simplification. 0 sets no limit.

Source:
Throws:

If an invalid limit is given

Type
external:Error
Returns:

true if simplification complete, false if there may still be some common factors left

Type
boolean

subtract(number)

Subtracts the given number from this number

Aliases: sub, minus

If this number is fractional, then it will perform a full fractional subtraction.
If it is set as an integer then the subtraction will ignore any fractional part of the number to be subtracted

Parameters:
Name Type Description
number number | string | module:TPA~Tpa

The number to subtract

Source:
Returns:

This number for chaining purposes

Type
module:TPA~Tpa

toDecimal(maxDecimalPlaces)

Outputs a decimal representation of this number

All Tpa numbers are rational and thus have a limited or recurring set of decimal places
Recurring decimals are notated in square brackets - e.g. 33.[3] for 33 and one third
If there are more decimals to output than the maximum requested the output is cut off and finishes with an ellipsis (...)

Parameters:
Name Type Argument Default Description
maxDecimalPlaces number <optional>
100

The maximum number of decimal places to give

Source:
See:
Returns:

The number in format: [-]nnn.ddd[rrr]

Type
string

toFraction()

Outputs this number in fractional representation: [-]nnn nnn/nnn

Source:
Returns:

The number in fractional form

Type
string

toInteger()

Outputs the decimal representation of the integer part of this number only

Source:
Returns:

The number in decimal form: [-]nnn

Type
string

toString(maxDecimalPlaces)

Outputs a decimal representation of this number

All Tpa numbers are rational and thus have a limited or recurring set of decimal places
Recurring decimals are notated in square brackets - e.g. 33.[3] for 33 and one third
If there are more decimals to output than the maximum requested the result is cut off andends with an ellipsis (...)

Parameters:
Name Type Argument Default Description
maxDecimalPlaces number <optional>
100

The maximum number of decimal places to give

Source:
See:
Returns:

The number in format: [-]nnn.ddd[rrr]

Type
string

value()

Gets the value of this number in standard JS floating point number

Note that precision may well be lost in order to accommodate the limitations of floating point numbers.
For this reason, the number of decimal places is restricted to 8.
Tpa numbers can be so large as to cause an overflow on a floating point number to yield infinity

Source:
Returns:

A numeric value of this number

Type
number