new Unitz.Fraction(value, denominators, largestDenominator, roundDown)
Instantiates and builds a fraction from a number given the denominators to use and the largest denominator to look at for this instance.
The fraction is calculated by using each of the denominators given and calculating the nearest fraction - the denoninator that results in the fraction closest to the given value is used. If no denominators are given or the value is closer to a whole number than to any other fraction then the fraction will have a denominator of one. If no fractions can be calculated that leaves the resulting value within {@Unitz.epsilon} then this fraction will be marked as "invalid".
new Unitz.Fraction( 0.5, [2, 3, 4] ).string; // '1/2'
new Unitz.Fraction( 1.5, [2, 3, 4] ).string; // '1 1/2'
new Unitz.Fraction( 0.25, [2, 3, 4] ).string; // '1/4'
new Unitz.Fraction( 0.125, [2, 3, 4] ).string; // 0.125
new Unitz.Fraction( 0.125, [2, 3, 4, 8] ).string; // '1/8'
Name | Type | Default | Description |
---|---|---|---|
value |
Number |
A number to build a fraction for. |
|
denominators |
Array.<Number> |
The array of denominators to use when converting the given value into a fraction. |
|
largestDenominator |
Number |
optional
Sometimes you don't want to use all of the denominators in the above array
(it could be from a |
|
roundDown |
Boolean | false |
optional
A fraction will try to find the closest value to |
Members
-
actualNumber
-
The actual value of the fraction (numerator / denominator).
-
denominatorNumber
-
The calculated denominator of the fraction. If this fraction is not valid this will be a 1 (valid fractions can also have a denoninator of 1 if the given
value
is a whole number). -
distanceNumber
-
The distance the calculated fraction value (
actual
) is from thevalue
given in the constructor. -
numeratorNumber
-
The calculated numerator of the fraction. If the fraction is negative this number will be negative. If this fraction is not valid - this will be the
value
passed into the constructor. -
remainder
-
The remainder is the part that goes over the denominator when the fraction has a whole number (when
|value|
is over 1). -
string
-
The string representation of this fraction. If the denominator is "1" or the fraction is invalid then the
numerator
is returned. If the fraction has a whole number component thenwhole remainder/denominator
is returned otherwisenumerator/denominator
is returned. -
valid
-
Whether or not the fraction is valid. A Fraction is valid when a fraction could be calculated that is within
Unitz.epsilon
of thevalue
given to the constructor. -
valueNumber
-
The
value
passed to the fraction constructor. -
wholeNumber
-
The whole number calculated for the fraction. This will be zero for values less than 1 and greater than -1.
Methods
-
isSingular(){Number}
-
Determines whether this fraction represents a singular value. Fractions are handled differently than numbers - a number must be equal to 1 or -1 but a fraction can be singular if between 1 and -1. This ensures that expressions like describing "0.5 miles" in fraction form "1/2 mile" makes sense.
Returns:
Returns 1 if this fraction describes a singular amount, otherwise 0.