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]
Parameters:
-
[numerator=0]Ratio | String | Number optionalcan be a Ratio object or numeric value.
-
[denominator=1]Ratio | String | Number optionalcan be a Ratio object or numeric value.
-
[alwaysReduce]Boolean optionalif true, then the Ratio object and the children from it will always represent the simplified form of the rational.
Returns:
Example:
Ratio(2,4).toString() === "2/4"; Ratio("2/4").toString() === "NaN/1" // Use Ratio.parse()!
Item Index
Methods
- Ratio.gcd
- Ratio.getCleanENotation
- Ratio.getCombinedRatio
- Ratio.getNumeratorWithSign
- Ratio.getPrimeFactors
- Ratio.getRepeatProps
- Ratio.getValueIfDefined
- Ratio.guessType
- Ratio.isNumeric
- Ratio.parse
- Ratio.parseToArray
- Ratio.prototype.abs
- Ratio.prototype.add
- Ratio.prototype.ceil
- Ratio.prototype.cleanFormat
- Ratio.prototype.clone
- Ratio.prototype.correctRatio
- Ratio.prototype.deepEquals
- Ratio.prototype.denominator
- Ratio.prototype.descale
- Ratio.prototype.divide
- Ratio.prototype.equals
- Ratio.prototype.findX
- Ratio.prototype.floor
- Ratio.prototype.isNaN
- Ratio.prototype.isProper
- Ratio.prototype.makeProper
- Ratio.prototype.mod
- Ratio.prototype.multiply
- Ratio.prototype.negate
- Ratio.prototype.numerator
- Ratio.prototype.pow
- Ratio.prototype.reciprocal
- Ratio.prototype.reduce deprecated
- Ratio.prototype.scale
- Ratio.prototype.simplify
- Ratio.prototype.subtract
- Ratio.prototype.toArray
- Ratio.prototype.toLocaleString
- Ratio.prototype.toQuantityOf
- Ratio.prototype.toString
- Ratio.prototype.valueOf
- Ratio.random
- Ratio.simplify
- Ratio.simplifyENotation
Methods
Ratio.gcd
-
a -
b
Find the Greatest Common Factor between two numbers using the Euler Method.
Parameters:
-
aNumber -
bNumber
Returns:
Example:
Ratio.gcd(20,12) === 4
Ratio.getCleanENotation
-
num
Rounds up a scientific notated number with 8+ trailing 0s or 9s.
Parameters:
-
numNumber
Returns:
- 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]
Used to combine two ratios into one.
Returns:
Example:
Ratio.getCombinedRatio("1/2","1/3").toString() === "3/2"
Ratio.getNumeratorWithSign
-
top -
bottom
Returns the numerator with the corresponding sign of (top/bottom).
Parameters:
-
topNumber -
bottomNumber
Returns:
Example:
Ratio.getNumeratorWithSign(1,-2) === -1
Ratio.getPrimeFactors
-
num
Returns the prime factors of a number.
More info http://bateru.com/news/2012/05/code-of-the-day-javascript-prime-factors-of-a-number/
Parameters:
-
numNumber
Returns:
Example:
Ratio.getPrimeFactors(20).join(',') === "2,2,5"
Ratio.getRepeatProps
-
val
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:
-
valNumber
Returns:
- Must return strings because of zeros in pattern.
Example:
Ratio.getRepeatProps( 22/7 ) // returns ["3", "14", "285714"]
Ratio.getValueIfDefined
-
backup -
value
Returns the default value if the provided new value is undefined or null.
Parameters:
-
backupObject- default value
-
valueObject
Returns:
Example:
Ratio.getValueIfDefined( 4, null ) === 4
Ratio.guessType
-
obj
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:
Example:
Ratio.guessType("1/3") === "fraction";
Ratio.isNumeric
-
obj
Checks if value is a finite number.
Borrowed from jQuery 1.7.2
Parameters:
-
objObject
Returns:
Example:
Ratio.isNumeric("1.0e3") === true
Ratio.parse
-
obj -
[obj]
Converts a numeric value to a Ratio object. Supports mixed numbers, whole numbers, decimals, scientific numbers and Ratio objects.
Parameters:
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
Converts a numeric value to an array in the form of [top, bottom], such that top/bottom evaluates to the passed value.
Parameters:
-
objNumber | StringNumeric Object.
Returns:
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]
Adds the current Ratio to another Ratio.
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]
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
Performs a strict comparison to determine if the current instances and passed object are identical.
Parameters:
-
objObject
Returns:
Example:
Ratio(1,2).deepEquals( 1/2 ) === false
Ratio.prototype.denominator
-
val
A setter and getter for the denominator
Parameters:
-
valRatio | String | Number- denominator
Returns:
- denominator
Ratio.prototype.descale
-
obj -
[obj2]
Returns an new Ratio scaled down by a factor from the current instance.
Returns:
Example:
Ratio(10,4).descale( 2 ).toString() === "5/2"
Ratio.prototype.divide
-
obj -
[obj2]
Divides the current Ratio by another Ratio.
Returns:
Example:
Ratio( 1,2 ).divide( 3,4 ).toString() === "4/6"
Ratio.prototype.equals
-
obj
Returns if the current Ratio and another object have the same numeric value.
Parameters:
-
objObject
Returns:
Example:
Ratio(1,2).equals( 1/2 ) === true
Ratio.prototype.findX
-
str
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:
-
strStringa 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:
Example:
Ratio(1,2).isNaN() === false;
Ratio.prototype.isProper
()
Boolean
Determines if the current Ratio is a proper fraction.
Returns:
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]
Multiply the current Ratio by another Ratio.
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
A setter and getter for the numerator
Parameters:
-
valRatio | String | Number- numerator
Returns:
- numerator
Ratio.prototype.pow
-
obj -
[obj2]
From the Ratio instance, returns an new Ratio raised to a power.
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]
From the Ratio instance, returns a new Ratio scaled up by a factor.
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]
Subtracts the current Ratio from another Ratio.
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:
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:
Example:
Example 1:
Ratio(1,10).toLocaleString() === "1/10"
Example 2:
Ratio(0,0).toLocaleString() === "NaN"
Ratio.prototype.toQuantityOf
-
base
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:
-
baseNumber, ...
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:
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:
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]
Returns an array of two numbers that represent ratio of the passed values.
Returns:
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
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:
-
topNumber -
bottomNumber
Returns:
- 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
