API Docs for:
Show:

Ratio Class

Ratio is an object that has a numerator and denominator, corresponding to a/b.
Note that the keyword new is not required to create a new instance of the Ratio object, since this is done for you.
In otherwords, new Ratio( value ) is the same as Ratio( value ).

Constructor

Ratio

(
  • [numerator=0]
  • [denominator=1]
  • [alwaysReduce]
)
Ratio chainable

Parameters:

  • [numerator=0] Ratio | String | Number optional

    can be a Ratio object or numeric value.

  • [denominator=1] Ratio | String | Number optional

    can be a Ratio object or numeric value.

  • [alwaysReduce] Boolean optional

    if true, then the Ratio object and the children from it will always represent the simplified form of the rational.

Returns:

Ratio:

object that has a numerator and denominator, corresponding to a/b.

Example:

Ratio(2,4).toString() === "2/4"; Ratio("2/4").toString() === "NaN/1" // Use Ratio.parse()!

Methods

Ratio.gcd

(
  • a
  • b
)
Number

Find the Greatest Common Factor between two numbers using the Euler Method. Will return the first argument if only one argument is passed.

Parameters:

  • a Number
  • b Number

Returns:

Number:

Example:

Ratio.gcd(20,12) === 4

Ratio.getCleanENotation

(
  • num
)
String

Rounds up a scientific notated number with 8+ trailing 0s or 9s.

Parameters:

  • num Number

Returns:

String:
  • Returns number as string to preserve value.

Example:

Example 1 Ratio.getCleanENotation( "1.1000000000000003e-30" ) === "1.1e-30";

Example 2 Ratio.getCleanENotation( "9.999999999999999e+22" ) === "1e+23";

Ratio.getCombinedRatio

(
  • [obj]
  • [obj]
)
Ratio chainable

Used to combine two ratios into one.

Parameters:

  • [obj] Ratio | String | Number optional
  • [obj] Ratio | String | Number optional

Returns:

Ratio:

[obj]

Example:

Ratio.getCombinedRatio("1/2","1/3").toString() === "3/2"

Ratio.getNumeratorWithSign

(
  • top
  • bottom
)
Number

Returns the numerator with the corresponding sign of (top/bottom).

Parameters:

  • top Number
  • bottom Number

Returns:

Number:

Example:

Ratio.getNumeratorWithSign(1,-2) === -1

Ratio.getPrimeFactors

(
  • num
)
Array

Parameters:

  • num Number

Returns:

Array:

an array of numbers

Example:

Ratio.getPrimeFactors(20).join(',') === "2,2,5"

Ratio.getRepeatProps

(
  • val
)
ArrayString, String, String

This function divides a repeating decimal into 3 parts. If the value passed is not a repeating decimal then an empty array is returned.
For repeating decimals, the return value is an array which contains the numeric value split into 3 parts like,
[ "numbers before decimal", "numbers before repeating pattern", "repeating pattern." ].
Here's another explanation.
The return value is [i, x, r] for the repeating decimal value.
where i are the values to the left of the decimal point.
x are the decimals to the right of the decimal point and to the left of the repeating pattern.
r is the unique repeating patterns for the repeating decimal.
Example. 22/7 = 3.142857142857143 = 3.14-285714-285714-3, i = 3, x = 14, r = 285714
It should be noted that the last digit might be removed to avoid rounding errors.

Parameters:

  • val Number

Returns:

ArrayString, String, String:
  • Must return strings because of zeros in pattern.

Example:

Ratio.getRepeatProps( 22/7 ) // returns ["3", "14", "285714"]

Ratio.getStandardRatioArray()

(
  • a
  • b
  • alwaysReduce
)
Array

Returns an ratio as an array with the first element containing the sign of the fraction.

Parameters:

  • a Number
    • numerator
  • b Number
    • denominator
  • alwaysReduce Boolean
    • if true, then returns the simplify fraction.

Returns:

Array:

[numerator, denominator]

Example:

Ratio.getStandardRatioArray(-10,-20, true); // returns [1,2]

Ratio.getValueIfDefined

(
  • backup
  • value
)
Object

Returns the default value if the provided new value is undefined or null.

Parameters:

  • backup Object
    • default value
  • value Object

Returns:

Object:

Example:

Ratio.getValueIfDefined( 4, null ) === 4

Ratio.guessType

(
  • obj
)
String

Provides a quick way to find out the numeric type of an object. Types include: NaN, Ratio, number, e, decimal, mixed and fraction

Parameters:

  • obj

Returns:

String:

type

Example:

Ratio.guessType("1/3") === "fraction";

Ratio.isNumeric

(
  • obj
)
Boolean

Checks if value is a finite number.
Borrowed from jQuery 1.7.2

Parameters:

  • obj Object

Returns:

Boolean:

Example:

Ratio.isNumeric("1.0e3") === true

Ratio.parse

(
  • obj
  • [obj]
)
Ratio chainable

Converts a numeric value to a Ratio object. Supports mixed numbers, whole numbers, decimals, scientific numbers and Ratio objects.

Parameters:

  • obj Ratio | Number | String
    • numerator
  • [obj] Ratio | Number | String optional
    • denominator

Returns:

Example:

Ratio.parse(22,7).toString() === "22/7"; //whole numbers

Ratio.parse("3 1/7").toString() === "22/7"; // mixed numbers

Ratio.parse(22/7).simplify().toLocaleString() === "3 1/7"; // decimals

Ratio.parse("22/7").toLocaleString() === "3 1/7"; // fractions

Ratio.parse("22e31/70e30").simplify().toLocaleString() === "3 1/7"; // scientific notated numbers

Ratio.parseToArray

(
  • obj
)
ArrayNumber, Number

Converts a numeric value to an array in the form of [top, bottom], such that top/bottom evaluates to the passed value.

Parameters:

  • obj Number | String

    Numeric Object.

Returns:

ArrayNumber, Number:

Example:

Ratio.parseToArray( 0.125 ) // returns [125, 1000]

Ratio.prototype.abs

() Ratio chainable

Returns a new instances that is the absolute value of the current Ratio.

Returns:

Example:

   Ratio(-3,2).abs().toString() === "3/2"

Ratio.prototype.add

(
  • obj
  • [obj2]
)
Ratio chainable

Adds the current Ratio to another Ratio.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio( 1, 3 ).add( 1,2 ).toString() === "5/6"

Ratio.prototype.ceil

() Ratio chainable

Returns a new Ratio from the ceil of the current Ratio instance.

Returns:

Example:

   Ratio.parse(4.2).ceil().toString() === "5/1"

Ratio.prototype.cleanFormat

() Ratio chainable

From the Ratio instance, returns a new Ratio by parsing the numerator and denominator.
This is useful if want to ensure that the Ratio contains only whole numbers in the numerator and denominator after a caclulation.

Returns:

Example:

   var a = Ratio(20,30).descale(3);
   a.toString() == "6.666666666666667/10";
   a.cleanFormat().toString() == "6666666666666667/10000000000000000"

Ratio.prototype.clone

(
  • [top]
  • [bottom]
  • [alwaysReduce]
)
Ratio

Returns a new instance of the current Ratio.
The clone propery value can be changed if the appropriate argument value is supplied.

Parameters:

  • [top] Number optional
  • [bottom] Number optional
  • [alwaysReduce] Boolean optional

Returns:

Example:

   var a = Ratio(2,4);
   var b = a.clone();
   a.equals(b) === true;

Ratio.prototype.correctRatio

() Ratio

For each ratio instance, corrects three main problems: 1) Sets the numerator and denominator to default values if undefined. (Default fraction: 0/1) 2) Places the sign on numerator. 3) Reduces the ratio if needed.

Returns:

Example:

   Ratio().toString() === "0/1"; // .correctRatio() was called internally.

Ratio.prototype.deepEquals

(
  • obj
)
Boolean

Performs a strict comparison to determine if the current instances and passed object are identical.

Parameters:

  • obj Object

Returns:

Boolean:

Example:

   Ratio(1,2).deepEquals( 1/2 ) === false

Ratio.prototype.denominator

(
  • val
)
Number

A setter and getter for the denominator

Parameters:

  • val Ratio | String | Number
    • denominator

Returns:

Number:
  • denominator

Ratio.prototype.descale

(
  • obj
  • [obj2]
)
Ratio chainable

Returns an new Ratio scaled down by a factor from the current instance.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio(10,4).descale( 2 ).toString() === "5/2"

Ratio.prototype.divide

(
  • obj
  • [obj2]
)
Ratio chainable

Divides the current Ratio by another Ratio.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio( 1,2 ).divide( 3,4 ).toString() === "4/6"

Ratio.prototype.equals

(
  • obj
)
Boolean

Returns if the current Ratio and another object have the same numeric value.

Parameters:

  • obj Object

Returns:

Boolean:

Example:

   Ratio(1,2).equals( 1/2 ) === true

Ratio.prototype.findX

(
  • str
)
Ratio chainable

Determines the value of x. Solves the following equations.

  1. ( a/b = x/n ) or
  2. ( a/b = n/x )
    Where a, b are the numerator and denominator respectively of the current Ratio.
    Note: Returns null if the the string can't be split into exactly 2 elements.

Parameters:

  • str String

    a string representing a fraction with a 'x' in the numerator or denominator.

Returns:

Example:

   Ratio(1,4).findX("x/20") == 5;

Ratio.prototype.floor

() Ratio chainable

Returns a new Ratio from the floor of the current Ratio instance.

Returns:

Example:

   Ratio.parse(4.2).floor().toString() === "4/1"

Ratio.prototype.isNaN

() Boolean

Determines if a current instance value is not a number.

Returns:

Boolean:

Example:

   Ratio(1,2).isNaN() === false;

Ratio.prototype.isProper

() Boolean

Determines if the current Ratio is a proper fraction.

Returns:

Boolean:

Example:

   Ratio(12,3).isProper() == false;

Ratio.prototype.makeProper

() Ratio chainable

Returns a new Ratio by removing the integer part of the current instance. In otherwords, returns the decimal portion as a fraction.

Returns:

Example:

   Ratio.parse(4.2).makeProper().toString() === "2/10"

Ratio.prototype.mod

() Ratio chainable

From the Ratio instance, returns a new Ratio in the form of (numerator mod denominator)/1.
Which is the same as Ratio( (numerator % denominator), 1 ).

Returns:

Example:

   Ratio(3,10).mod().toString() === "3/1"

Ratio.prototype.multiply

(
  • obj
  • [obj2]
)
Ratio chainable

Multiply the current Ratio by another Ratio.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio(2,5).multiply( 1, 2 ).toString() === "2/10"

Ratio.prototype.negate

() Ratio chainable

Returns a new instance of the Ratio with the sign toggled.

Returns:

Example:

   Ratio(1,2).negate().toString() === "-1/2"

Ratio.prototype.numerator

(
  • val
)
Number

A setter and getter for the numerator

Parameters:

  • val Ratio | String | Number
    • numerator

Returns:

Number:
  • numerator

Ratio.prototype.pow

(
  • obj
  • [obj2]
)
Ratio chainable

From the Ratio instance, returns an new Ratio raised to a power.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio(2,4).pow(4).toString() === "16/256"

Ratio.prototype.reciprocal

() Ratio chainable

Switches the numerator and denominator positions.

Returns:

Example:

   Ratio(1,2).reciprocal().toString() == "2/1";

Ratio.prototype.reduce

() deprecated

Use Ratio.prototype.simplify() instead.

Ratio.prototype.scale

(
  • obj
  • [obj2]
)
Ratio chainable

From the Ratio instance, returns a new Ratio scaled up by a factor.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio(1,10).scale(10).toString() === "10/100"

Ratio.prototype.simplify

() Ratio chainable

Returns a simplifyd ratio from the current instance.

Returns:

Example:

   Ratio(10,2).simplify().toString() === "5/1"

Ratio.prototype.subtract

(
  • obj
  • [obj2]
)
Ratio chainable

Subtracts the current Ratio from another Ratio.

Parameters:

  • obj Ratio | Number | String
  • [obj2] Ratio | Number | String optional

Returns:

Example:

   Ratio(2,3).subtract(1,7).toString() === "11/21"

Ratio.prototype.toArray

() Array

From the Ratio instance, returns the raw values of the numerator and denominator in the form [numerator, denominator].

Returns:

Array:

an array of 2 numbers.

Example:

   Ratio(1,2).toArray().join(',') === "1,2"

Ratio.prototype.toLocaleString

() String

From the Ratio instance, returns a string of the Ratio in fraction form if the numerator and denominator are Rational numbers.
The output format can be a whole number, mixed number, NaN, proper fraction depending on the computed value of (numerator / denominator).

Returns:

String:

Example:

   Example 1:
   Ratio(1,10).toLocaleString() === "1/10"

   Example 2:
   Ratio(0,0).toLocaleString() === "NaN"

Ratio.prototype.toQuantityOf

(
  • base
)
Ratio chainable

From the Ratio instance, approxiates the value to a new fraction with a provided denominator. In otherwords, this method helps you find out what fraction with a given denominator will best represent the current numeric value of the Ratio. Operates on a arbitary amount of arguments and returns the Ratio with the closest match among the quantities. Therefore, an approximated quantity is returned if the absolute value of the difference between the approximated quantity and actual value is smaller than the error rate.

Parameters:

  • base Number, ...

Returns:

Example:

   Ratio(27,100).toQuantityOf(3).toString() == "1/3";
   Ratio(1,2).toQuantityOf(2,3,4).toString() === "1/2";

Ratio.prototype.toString

() String

From the Ratio instance, returns the raw values of the numerator and denominator in the form "a/b".
Note: The division symbol can be change by modification of the divSign property.

Returns:

String:

Example:

   Example 1:
   Ratio(8,2).toString() === "8/2";

   Example 2:
   var a = Ratio(8,2);
   a.divSign = ":";
   a.toString() == "8:2";

Ratio.prototype.valueOf

() Number

From the Ratio instance, returns the result of the numerator divided by the denominator.

Returns:

Number:

Example:

   Example 1:
   Ratio(1,2).valueOf() === 0.5;

Ratio.random

() Ratio chainable

Returns a new Ratio with random values for the numerator and denominator. Values range from [0, 1]

Returns:

Example:

Ratio.random().toString(); // might return "1/4"

Ratio.simplify

(
  • obj
  • [obj]
)
Array Number, Number

Returns an array of two numbers that represent ratio of the passed values.

Parameters:

  • obj Ratio | Number | String
  • [obj] Ratio | Number | String optional

Returns:

Array Number, Number :

Example:

Example 1:

Ratio.simplify( Ratio(36,-36) ); // returns [-1,1]

Example 2:

Ratio.simplify( "9/12" ); // returns [3,4]

Example 3:

Ratio.simplify( "10/4", "5/3" ); // returns [3,2] because ("10/4", "5/3") => ("6/4") Ratio.simplify( "6/4" ); // returns [3,2]

Ratio.simplifyENotation

(
  • top
  • bottom
)
Array

Moves all the zeros for scientific numbers to the first or second element.
The most takes all. This helps simplify computational errors with SNs. Need to reword

Parameters:

  • top Number
  • bottom Number

Returns:

Array:
  • Pair of numbers

Example:

Ratio.simplifyENotation(3e80,3e35); // returns [3e45,3]

Properties

Ratio.MAX_PRECISION

Number

Represents the maximum amount of precision avaiable.
Any value with more digits will become estimations.

Ratio.MAX_VALUE

Number

Represents the largest value that stored without loss of precision.
Any value larger will become estimations. Source: "http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t"

Ratio.MIN_VALUE

Number

Represents the smallest value that stored without loss of precision.
Any value smaller will become estimations. Source: "http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t"

Ratio.regex

Object

Stores complex regular expressions.

Ratio.VERSION

String

Version number of Ratio.js