com.github.kiprobinson.bigfraction
Class LongFraction

java.lang.Object
  extended by java.lang.Number
      extended by com.github.kiprobinson.bigfraction.LongFraction
All Implemented Interfaces:
Serializable, Comparable<Number>

public final class LongFraction
extends Number
implements Comparable<Number>

Arbitrary-precision fraction, utilizing longs for numerator and denominator. Fraction is always kept in lowest terms. Fraction is immutable, and guaranteed not to have a null numerator or denominator. Denominator will always be positive (so sign is carried by numerator, and a zero-denominator is impossible).

Because mathematical operations are done natively, they will perform much better than with BigFraction, but with the risk of overflow.

Any operations that overflow throw an ArithmeticException.

See Also:
Serialized Form

Field Summary
static LongFraction ONE
          The value 1/1.
static LongFraction ONE_HALF
          The value 1/2.
static LongFraction ONE_TENTH
          The value 1/10.
static LongFraction TEN
          The value 10/1.
static LongFraction ZERO
          The value 0/1.
 
Constructor Summary
LongFraction(Number n)
          Note: valueOf(Number) should be preferred for performance reasons.
LongFraction(Number numerator, Number denominator)
          Note: valueOf(Number, Number) should be preferred for performance reasons.
LongFraction(String s)
          Note: valueOf(String) should be preferred for performance reasons.
LongFraction(String s, int radix)
          Note: valueOf(String, int) should be preferred for performance reasons.
 
Method Summary
 LongFraction abs()
          Returns the absolute value of this.
 LongFraction add(Number n)
          Returns this + n.
 byte byteValue()
          Returns a byte representation of this fraction.
 byte byteValueExact()
          Returns an exact byte representation of this fraction.
 int compareTo(Number n)
          Returns a negative, zero, or positive number, indicating if this object is less than, equal to, or greater than n, respectively.
 LongFraction complement()
          Returns the complement of this fraction, which is equal to 1 - this.
static LongFraction difference(Number a, Number b)
          Returns a - b, represented as a LongFraction.
 LongFraction divide(Number n)
          Returns this / n.
 Number[] divideAndRemainder(Number n)
          Returns integral quotient and fractional remainder of this/n.
 Number[] divideAndRemainder(Number n, DivisionMode divisionMode)
          Returns integral quotient and fractional remainder of this/n, using specified division mode.
 LongFraction divideInto(Number n)
          Returns n / this.
 long divideToIntegralValue(Number n)
          Divides to an integral value, using DivisionMode.TRUNCATED division mode.
 long divideToIntegralValue(Number n, DivisionMode divisionMode)
          Divides to an integral value, using the specified division mode.
 double doubleValue()
          Returns the value of this fraction.
 double doubleValueExact()
          Returns an exact double representation of this fraction.

Warning: Current algorithm is simply to convert to double, then convert back to BigFraction, then make sure the copy of copy is identical to the original.
 boolean equals(Object o)
          Returns if this object is equal to another object.
 boolean equalsNumber(Number n)
          Returns if this object is equal to another Number object.
 LongFraction fareyClosest(int maxDenominator)
          Returns the closest fraction with denominator less than or equal to the given denominator.
 LongFraction fareyNext(int maxDenominator)
          Returns the next fraction in the Farey sequence with denominator less than or equal to the given denominator.
 LongFraction fareyPrev(int maxDenominator)
          Returns the previous fraction in the Farey sequence with denominator less than or equal to the given denominator.
 float floatValue()
          Returns the value of this fraction.
 float floatValueExact()
          Returns an exact float representation of this fraction.

Warning: Current algorithm is simply to convert to float, then convert back to BigFraction, then make sure the copy of copy is identical to the original.
 LongFraction gcd(Number n)
          Returns the greatest common divisor (also called greatest common factor) of this and n.

If this and n are both zero, returns 0/1.

Note: The result will always be nonnegative, regardless of the signs of the inputs.

When dealing with fractions, the divisors of a/b are: (a/b)/1, (a/b)/2, (a/b)/3, ...
 long getDenominator()
          Returns the denominator of this fraction.
 LongFraction getFractionPart()
          Returns the fraction part of this fraction; that is, the fraction that represents the part that would come after the decimal point if this were written as a decimal number.
 LongFraction getFractionPart(DivisionMode divisionMode)
          Returns the fraction part of this fraction; that is, the remainder when the numerator is divided by the denominator when using the specified division mode.
 long getIntegerPart()
          Returns the integer part of this fraction; that is, the part that would come before the decimal point if this were written as a decimal number.
 long getIntegerPart(DivisionMode divisionMode)
          Returns the integer part of this fraction; that is, the part that would come before the decimal point if this were written as a decimal number.
 long getNumerator()
          Returns the numerator of this fraction.
 Number[] getParts()
          Returns the integer and fraction parts of this fraction.
 Number[] getParts(DivisionMode divisionMode)
          Returns the integer and fraction parts of this fraction.
 int hashCode()
          Returns a hash code for this object.
static long integralQuotient(Number a, Number b)
          Returns result of integer division a / b, using truncated division mode.
static long integralQuotient(Number a, Number b, DivisionMode divisionMode)
          Returns result of integer division a / b, using specified division mode.
 int intValue()
          Returns an int representation of this fraction.
 int intValueExact()
          Returns an exact int representation of this fraction.
 LongFraction lcm(Number n)
          Returns least common multiple of this and n.

If this or n is zero, returns 0/1.

Note: The result will always be nonnegative, regardless of the signs of the inputs.

When dealing with fractions, the multiples of a/b are: (a/b)*1, (a/b)*2, (a/b)*3, ...
 long longValue()
          Returns a long representation of this fraction.
 long longValueExact()
          Returns an exact long representation of this fraction.
 Number max(Number n)
          Returns the larger of this and n.
static Number max(Number a, Number b)
          Returns the larger of a and b.
 LongFraction mediant(Number n)
          Returns the mediant of this and n.
static LongFraction mediant(Number a, Number b)
          Returns the mediant of a and b.
 Number min(Number n)
          Returns the smaller of this and n.
static Number min(Number a, Number b)
          Returns the smaller of a and b.
 LongFraction multiply(Number n)
          Returns this * n.
 LongFraction negate()
          Returns -this.
 LongFraction pow(int exponent)
          Returns this^exponent.

Note: 0^0 will return 1/1.
static LongFraction product(Number a, Number b)
          Returns a * b, represented as a LongFraction.
static LongFraction quotient(Number a, Number b)
          Returns a / b, represented as a LongFraction.
static Number[] quotientAndRemainder(Number a, Number b)
          Returns integral quotient and fractional remainder of integer division a / b, using truncated division mode.
static Number[] quotientAndRemainder(Number a, Number b, DivisionMode divisionMode)
          Returns integral quotient and fractional remainder of integer division a / b, using specified division mode.
 LongFraction reciprocal()
          Returns 1/this.
 LongFraction remainder(Number n)
          Computes division remainder (modulus), using DivisionMode.TRUNCATED division mode.
 LongFraction remainder(Number n, DivisionMode divisionMode)
          Computes division remainder (modulus), using the specified division mode.
static LongFraction remainder(Number a, Number b)
          Returns fractional remainder of integer division a / b, using truncated division mode.
static LongFraction remainder(Number a, Number b, DivisionMode divisionMode)
          Returns fractional remainder of integer division a / b, using specified division mode.
 long round()
          Returns this rounded to the nearest whole number, using RoundingMode.HALF_UP as the default rounding mode.
 long round(RoundingMode roundingMode)
          Returns this fraction rounded to a whole number, using the given rounding mode.
 long roundToDenominator(long newDenominator)
          Rounds the given fraction to the nearest fraction having the given denominator, using HALF_UP rounding method, and returns the numerator of that fraction.
 long roundToDenominator(long newDenominator, RoundingMode roundingMode)
          Rounds the given fraction to the nearest fraction having the given denominator, using the given rounding method, and returns the numerator of that fraction.

For example, given the fraction 7/15, if you wanted to know the nearest fraction with denominator 6, it would be 2.8/6, which rounds to 3/6.
 LongFraction roundToNumber(Number n)
          Rounds this fraction to the nearest multiple of the given number, using HALF_UP rounding method.
 LongFraction roundToNumber(Number n, RoundingMode roundingMode)
          Rounds this fraction to the nearest multiple of the given number, using the specified rounding method.

Note for HALF_EVEN rounding method: this rounds to the nearest even multiple of n, which may or may not be even.
 short shortValue()
          Returns a short representation of this fraction.
 short shortValueExact()
          Returns an exact short representation of this fraction.
 int signum()
          Returns -1, 0, or 1, representing the sign of this fraction.
 LongFraction subtract(Number n)
          Returns this - n.
 LongFraction subtractFrom(Number n)
          Returns n - this.
static LongFraction sum(Number a, Number b)
          Returns a + b, represented as a LongFraction.
 BigDecimal toBigDecimal()
          Returns a BigDecimal representation of this fraction.

If possible, the returned value will be exactly equal to the fraction.
 BigDecimal toBigDecimal(int precision)
          Returns a BigDecimal representation of this fraction, with a given precision.
 String toDecimalString(int numDecimalDigits)
          Returns decimal string representation of the fraction with the given number of decimal digits using roundingMode ROUND_HALF_UP.
 String toDecimalString(int numDecimalDigits, RoundingMode roundingMode)
          Converts the fraction to a string with the given number of decimal digits.
 String toMixedString()
          Returns string representation of this object as a mixed fraction.
 String toMixedString(int radix)
          Returns string representation of this object as a mixed fraction.
 String toRadixedString(int radix, int numFractionalDigits)
          Converts the fraction to a radixed string with the given number of fraction digits after the radix point.
 String toRadixedString(int radix, int numFractionalDigits, RoundingMode roundingMode)
          Converts the fraction to a radixed string with the given number of fraction digits after the radix point.

For example, 1/8 in base 10 is 0.125.
 String toRepeatingDigitString()
          Converts the fraction to a radixed string with repeating digits.
 String toRepeatingDigitString(boolean forceRepeating)
          Converts the fraction to a radixed string with repeating digits.
 String toRepeatingDigitString(int radix)
          Converts the fraction to a radixed string with repeating digits, in the given radix.
 String toRepeatingDigitString(int radix, boolean forceRepeating)
          Converts the fraction to a radixed string with repeating digits, in the given radix.
 String toString()
          Returns a string representation of this, in the form numerator/denominator.
 String toString(boolean denominatorOptional)
          Returns a string representation of this, in the form of numerator/denominator.
 String toString(int radix)
          Returns string representation of this, in the form of numerator/denominator, with numerator and denominator represented in the given radix.
 String toString(int radix, boolean denominatorOptional)
          Returns string representation of this, in the form of numerator/denominator, with numerator and denominator represented in the given radix.
static LongFraction valueOf(Number n)
          Constructs a LongFraction from given number.
static LongFraction valueOf(Number numerator, Number denominator)
          Constructs a LongFraction with given numerator and denominator.
static LongFraction valueOf(String s)
          Constructs a LongFraction from a String.
static LongFraction valueOf(String s, int radix)
          Constructs a LongFraction from a String.
 LongFraction withSign(int sgn)
          Returns this, with sign set to the sign of sgn parameter.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final LongFraction ZERO
The value 0/1.


ONE

public static final LongFraction ONE
The value 1/1.


ONE_HALF

public static final LongFraction ONE_HALF
The value 1/2.


ONE_TENTH

public static final LongFraction ONE_TENTH
The value 1/10.


TEN

public static final LongFraction TEN
The value 10/1.

Constructor Detail

LongFraction

public LongFraction(Number n)
Note: valueOf(Number) should be preferred for performance reasons. This constructor is provided for convenience.

Parameters:
n - Number to convert to LongFraction.
See Also:
valueOf(Number)

LongFraction

public LongFraction(Number numerator,
                    Number denominator)
Note: valueOf(Number, Number) should be preferred for performance reasons. This constructor is provided for convenience.

Parameters:
numerator - numerator of new LongFraction
denominator - denominator of new LongFraction
See Also:
valueOf(Number, Number)

LongFraction

public LongFraction(String s)
Note: valueOf(String) should be preferred for performance reasons. This constructor is provided for convenience.

Parameters:
s - String to convert to parse as LongFraction
See Also:
valueOf(String)

LongFraction

public LongFraction(String s,
                    int radix)
Note: valueOf(String, int) should be preferred for performance reasons. This constructor is provided for convenience.

Parameters:
s - String to convert to parse as LongFraction
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
See Also:
valueOf(String, int)
Method Detail

valueOf

public static LongFraction valueOf(Number n)
Constructs a LongFraction from given number. If the number is not one of the known implementations of Number class, then Number.doubleValue() will be used for construction.

Warning: when using floating point numbers, round-off error can result in answers that are unexpected. For example:
System.out.println(LongFraction.valueOf(1.1))
will print:
2476979795053773/2251799813685248

This is because 1.1 cannot be expressed exactly in binary form. The computed fraction is exactly equal to the internal representation of the double-precision floating-point number. (Which, for 1.1, is: (-1)^0 * 2^0 * (1 + 0x199999999999aL / 0x10000000000000L).)

In many cases, LongFraction.valueOf(Double.toString(d)) may give the result the user expects.

Parameters:
n - Any Number to be converted to a LongFraction
Returns:
a fully reduced fraction equivalent to n. Guaranteed to be non-null.
Throws:
IllegalArgumentException - if n is null.

valueOf

public static LongFraction valueOf(Number numerator,
                                   Number denominator)
Constructs a LongFraction with given numerator and denominator. Fraction will be reduced to lowest terms. If fraction is negative, negative sign will be carried on numerator, regardless of how the values were passed in. The numerator and denominator can both be non-integers.

Example: LongFraction.valueOf(8.5, -6.25); //-34/25

Warning: when using floating point numbers, round-off error can result in answers that are unexpected. For example,
System.out.println(LongFraction.valueOf(1.1))

This is because 1.1 cannot be expressed exactly in binary form. The computed fraction is exactly equal to the internal representation of the double-precision floating-point number. (Which, for 1.1, is: (-1)^0 * 2^0 * (1 + 0x199999999999aL / 0x10000000000000L).)

In many cases, LongFraction.valueOf(Double.toString(d)) may give the result the user expects.

Parameters:
numerator - any Number to be used as the numerator. This does not need to be an integer.
denominator - any Number to be used as the denominator. This does not need to be an integer.
Returns:
a fully reduced fraction equivalent to numerator/denominator. Guaranteed to be non-null.
Throws:
ArithmeticException - if denominator == 0.
IllegalArgumentException - if numerator or denominator is null.

valueOf

public static LongFraction valueOf(String s)
Constructs a LongFraction from a String. Expected format is numerator/denominator, but "/denominator" part is optional. Either numerator or denominator may be a floating-point decimal number, which is in the same format as a parameter to the BigDecimal.BigDecimal(String) constructor.

Numerator or denominator can also be expressed as a repeating decimal, such as 0.(1) = 0.1111... Scientific notation is not allowed when using repeating digits.

Examples:
LongFraction.valueOf("11"); //11/1
LongFraction.valueOf("22/34"); //11/17
LongFraction.valueOf("2e4/-0.64"); //-174375/4
LongFraction.valueOf("0.(1)"); //1/9
LongFraction.valueOf("12.34(56)"); //122222/9900

Parameters:
s - a string representation of a number or fraction
Returns:
a fully reduced fraction equivalent to the specified string. Guaranteed to be non-null.
Throws:
NumberFormatException - if the string cannot be properly parsed.
ArithmeticException - if denominator == 0.
IllegalArgumentException - if s is null.
See Also:
BigDecimal.BigDecimal(String)

valueOf

public static LongFraction valueOf(String s,
                                   int radix)
Constructs a LongFraction from a String. Expected format is numerator/denominator, but "/denominator" part is optional.

If radix == 10: either numerator or denominator may be a floating-point decimal number, which is in the same format as a parameter to the BigDecimal.BigDecimal(String) constructor.

If radix != 10: the numerator and denominator may be a radixed string string in that base, but cannot contain a scientific notation exponent. The numerator and denominator must be in a format that, with the radix point removed, can be parsed by the BigInteger.BigInteger(String, int) constructor. This means that scientific notation is not allowed in bases other than 10.

Numerator or denominator can also be expressed as a radixed string with a repeating digit, such as 0.(1) = 0.1111... This is allowed with any radix. Scientific notation is not allowed when using repeating digits.

Examples:
LongFraction.valueOf("11", 10); //11/1
LongFraction.valueOf("22/34", 10); //11/17
LongFraction.valueOf("2e4/-0.64", 10); //-174375/4
LongFraction.valueOf("dead/beef", 16); //57005/48879
LongFraction.valueOf("lazy.fox", 36); //15459161339/15552
LongFraction.valueOf("0.(1)", 10); //1/9
LongFraction.valueOf("12.34(56)", 10); //122222/9900
LongFraction.valueOf("0.(1)", 16); //1/15
LongFraction.valueOf("the.lazy(fox)", 36); //2994276908470787/78362484480

Parameters:
s - a string representation of a number or fraction
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
Returns:
a fully reduced fraction equivalent to the specified string. Guaranteed to be non-null.
Throws:
NumberFormatException - if the string cannot be properly parsed.
ArithmeticException - if denominator == 0.
IllegalArgumentException - if s is null.
See Also:
BigDecimal.BigDecimal(String), BigInteger.BigInteger(String, int)

getNumerator

public final long getNumerator()
Returns the numerator of this fraction.

Returns:
numerator of this fraction.

getDenominator

public final long getDenominator()
Returns the denominator of this fraction.

Returns:
denominator of this fraction.

add

public LongFraction add(Number n)
Returns this + n.

Parameters:
n - number to be added to this
Returns:
this + n
Throws:
IllegalArgumentException - if n is null.

sum

public static LongFraction sum(Number a,
                               Number b)
Returns a + b, represented as a LongFraction. Equivalent to LongFraction.valueOf(a).add(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number to be added
b - number to be added
Returns:
a + b
Throws:
IllegalArgumentException - if a or b is null.

subtract

public LongFraction subtract(Number n)
Returns this - n.

Parameters:
n - number to be subtracted from this
Returns:
this - n
Throws:
IllegalArgumentException - if n is null.

subtractFrom

public LongFraction subtractFrom(Number n)
Returns n - this. Sometimes this results in cleaner code than rearranging the code to use subtract().

Parameters:
n - number to subtract this from
Returns:
n - this
Throws:
IllegalArgumentException - if n is null.

difference

public static LongFraction difference(Number a,
                                      Number b)
Returns a - b, represented as a LongFraction. Equivalent to LongFraction.valueOf(a).subtract(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number to subtract from (minuend)
b - number to be subtracted from a (subtrahend)
Returns:
a - b
Throws:
IllegalArgumentException - if a or b is null.

multiply

public LongFraction multiply(Number n)
Returns this * n.

Parameters:
n - number to be multiplied by this
Returns:
this * n
Throws:
IllegalArgumentException - if n is null.

product

public static LongFraction product(Number a,
                                   Number b)
Returns a * b, represented as a LongFraction. Equivalent to LongFraction.valueOf(a).multiply(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number to be multiplied
b - number to be multiplied
Returns:
a * b
Throws:
IllegalArgumentException - if a or b is null.

divide

public LongFraction divide(Number n)
Returns this / n.

Parameters:
n - number to divide this by (divisor)
Returns:
this / n
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.

divideInto

public LongFraction divideInto(Number n)
Returns n / this. Sometimes this results in cleaner code than rearranging the code to use divide().

Parameters:
n - number to be divided by this (dividend)
Returns:
n / this
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if this == 0.

quotient

public static LongFraction quotient(Number a,
                                    Number b)
Returns a / b, represented as a LongFraction. Equivalent to LongFraction.valueOf(a).divide(b). Also equivalent to LongFraction.valueOf(a, b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number to be divided (dividend)
b - number by which to divide (divisor)
Returns:
a / b
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.

divideToIntegralValue

public long divideToIntegralValue(Number n)
Divides to an integral value, using DivisionMode.TRUNCATED division mode.

Parameters:
n - number to divide this by (dividend)
Returns:
integral quotient of this / n, using truncated division
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
divideAndRemainder(Number, DivisionMode), DivisionMode

divideToIntegralValue

public long divideToIntegralValue(Number n,
                                  DivisionMode divisionMode)
Divides to an integral value, using the specified division mode.

Parameters:
n - number to divide this by (dividend)
divisionMode - division mode to use if dividend or divisor is negative.
Returns:
integral quotient of this / n, using specified division mode.
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
divideAndRemainder(Number, DivisionMode), DivisionMode

remainder

public LongFraction remainder(Number n)
Computes division remainder (modulus), using DivisionMode.TRUNCATED division mode.

Parameters:
n - number to divide this by (dividend)
Returns:
division remainder (modulus) of this / n, using truncated division.
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
divideAndRemainder(Number, DivisionMode), DivisionMode

remainder

public LongFraction remainder(Number n,
                              DivisionMode divisionMode)
Computes division remainder (modulus), using the specified division mode.

Parameters:
n - number to divide this by (dividend)
divisionMode - division mode to use if dividend or divisor is negative.
Returns:
division remainder (modulus) of this / n, using specified division mode.
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
divideAndRemainder(Number, DivisionMode), DivisionMode

divideAndRemainder

public Number[] divideAndRemainder(Number n)
Returns integral quotient and fractional remainder of this/n. Uses DivisionMode.TRUNCATED division mode.

Parameters:
n - number to divide this by (dividend)
Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction.
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
divideAndRemainder(Number, DivisionMode), DivisionMode

divideAndRemainder

public Number[] divideAndRemainder(Number n,
                                   DivisionMode divisionMode)
Returns integral quotient and fractional remainder of this/n, using specified division mode. If the quotient and remainder are q and r, respectively, then the results satisfy the following equations:

this/n = q + r/n
this = q*n + r

Parameters:
n - Number to divide this by.
divisionMode - Division mode to use if dividend or divisor is negative.
Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction.
Throws:
IllegalArgumentException - if n is null.
ArithmeticException - if n == 0.
See Also:
DivisionMode

integralQuotient

public static long integralQuotient(Number a,
                                    Number b)
Returns result of integer division a / b, using truncated division mode. Equivalent to LongFraction.valueOf(a).divideToIntegralValue(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
Returns:
integral quotient of a / b, using TRUNCATED division mode
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
divideToIntegralValue(Number n), DivisionMode

integralQuotient

public static long integralQuotient(Number a,
                                    Number b,
                                    DivisionMode divisionMode)
Returns result of integer division a / b, using specified division mode. Equivalent to LongFraction.valueOf(a).divideToIntegralValue(b, divisionMode). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
divisionMode - division mode to use when dividend or divisor is negative.
Returns:
integral quotient of a / b, using specified division mode
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
divideToIntegralValue(Number n, DivisionMode divisionMode), DivisionMode

remainder

public static LongFraction remainder(Number a,
                                     Number b)
Returns fractional remainder of integer division a / b, using truncated division mode. Equivalent to LongFraction.valueOf(a).remainder(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
Returns:
division remainder (modulus) of a / b, using TRUNCATED division mode
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
remainder(Number n), DivisionMode

remainder

public static LongFraction remainder(Number a,
                                     Number b,
                                     DivisionMode divisionMode)
Returns fractional remainder of integer division a / b, using specified division mode. Equivalent to LongFraction.valueOf(a).remainder(b, divisionMode). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
divisionMode - division mode to use when dividend or divisor is negative.
Returns:
division remainder (modulus) of a / b, using specified division mode
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
remainder(Number n, DivisionMode divisionMode), DivisionMode

quotientAndRemainder

public static Number[] quotientAndRemainder(Number a,
                                            Number b)
Returns integral quotient and fractional remainder of integer division a / b, using truncated division mode. Equivalent to LongFraction.valueOf(a).divideAndRemainder(b). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction.
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
divideAndRemainder(Number n), DivisionMode

quotientAndRemainder

public static Number[] quotientAndRemainder(Number a,
                                            Number b,
                                            DivisionMode divisionMode)
Returns integral quotient and fractional remainder of integer division a / b, using specified division mode. Equivalent to LongFraction.valueOf(a).divideAndRemainder(b, divisionMode). Provided as static method to make code easier to write in some instances.

Parameters:
a - number be divided (dividend)
b - number to divide by (divisor)
divisionMode - division mode to use when dividend or divisor is negative.
Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction.
Throws:
IllegalArgumentException - if a or b is null.
ArithmeticException - if b == 0.
See Also:
divideAndRemainder(Number n, DivisionMode divisionMode), DivisionMode

gcd

public LongFraction gcd(Number n)
Returns the greatest common divisor (also called greatest common factor) of this and n.

If this and n are both zero, returns 0/1.

Note: The result will always be nonnegative, regardless of the signs of the inputs.

When dealing with fractions, the divisors of a/b are: (a/b)/1, (a/b)/2, (a/b)/3, ... (a/b)/n. Thus gcd(n1/d1, n2/d2) gives the largest fraction n3/d3, such that (n1/d1)/(n3/d3) is an integer, and (n2/d2)/(n3/d3) is an integer.

Parameters:
n - other value to compute gcd from.
Returns:
greatest common divisor of this and n.

lcm

public LongFraction lcm(Number n)
Returns least common multiple of this and n.

If this or n is zero, returns 0/1.

Note: The result will always be nonnegative, regardless of the signs of the inputs.

When dealing with fractions, the multiples of a/b are: (a/b)*1, (a/b)*2, (a/b)*3, ... (a/b)*n. Thus lcm(n1/d1, n2/d2) gives the smallest fraction n3/d3, such that (n3/d3)/(n1/d1) is an integer, and (n3/d3)/(n2/d2) is an integer.

Parameters:
n - other value to compute lcm from.
Returns:
least common multiple of this and n

pow

public LongFraction pow(int exponent)
Returns this^exponent.

Note: 0^0 will return 1/1. This is consistent with Math.pow(double, double), BigInteger.pow(int), and BigDecimal.pow(int).

Parameters:
exponent - power to raise this fraction to.
Returns:
this^exponent
Throws:
ArithmeticException - if this == 0 && exponent < 0.

reciprocal

public LongFraction reciprocal()
Returns 1/this.

Returns:
1/this
Throws:
ArithmeticException - if this == 0.

complement

public LongFraction complement()
Returns the complement of this fraction, which is equal to 1 - this. Useful for probabilities/statistics.

Returns:
1-this

negate

public LongFraction negate()
Returns -this. If this is zero, returns zero.

Returns:
equivalent of this.multiply(-1)

abs

public LongFraction abs()
Returns the absolute value of this.

Returns:
absolute value of this.

withSign

public LongFraction withSign(int sgn)
Returns this, with sign set to the sign of sgn parameter. Another way of saying it: returns the equivalent of this.abs().multiply(Math.signum(sgn)).

Important Note: If this is zero, always returns zero. No exception thrown, even if we are trying to set the sign of 0 to positive or negative.

Parameters:
sgn - an integer less than, equal to, or greater than 0, whose sign will be assigned to the returned fraction.
Returns:
equivalent of this.abs().multiply(Math.signum(sgn)).

signum

public int signum()
Returns -1, 0, or 1, representing the sign of this fraction.

Returns:
-1, 0, or 1, representing the sign of this fraction.

getIntegerPart

public long getIntegerPart()
Returns the integer part of this fraction; that is, the part that would come before the decimal point if this were written as a decimal number. Carries the same sign as this fraction.

Equivalent to getIntegerPart(DivisionMode.TRUNCATED)

Returns:
integer part of this fraction (numerator/denominator), using TRUCATED division mode.
See Also:
getParts(DivisionMode divisionMode)

getIntegerPart

public long getIntegerPart(DivisionMode divisionMode)
Returns the integer part of this fraction; that is, the part that would come before the decimal point if this were written as a decimal number. Carries the same sign as this fraction.

Parameters:
divisionMode - Division mode to use when computing quotient. Only relevant if this is negative.
Returns:
integer part of this fraction (numerator/denominator), using specified division mode.
See Also:
getParts(DivisionMode divisionMode)

getFractionPart

public LongFraction getFractionPart()
Returns the fraction part of this fraction; that is, the fraction that represents the part that would come after the decimal point if this were written as a decimal number. Carries the same sign as this fraction, unless the fraction part is zero.

Equivalent to getFractionPart(DivisionMode.TRUNCATED)

Returns:
fractional part of this fraction (i.e. (numerator%denominator)/denominator)), using TRUNCATED division mode.
See Also:
getParts(DivisionMode divisionMode)

getFractionPart

public LongFraction getFractionPart(DivisionMode divisionMode)
Returns the fraction part of this fraction; that is, the remainder when the numerator is divided by the denominator when using the specified division mode.

Parameters:
divisionMode - Division mode to use when computing remainder. Only relevant if this is negative.
Returns:
fractional part of this fraction (i.e. (numerator%denominator)/denominator)), using specified division mode.
See Also:
getParts(DivisionMode divisionMode)

getParts

public Number[] getParts()
Returns the integer and fraction parts of this fraction. The return array is guaranteed to have exactly two elements. The first is guaranteed to be a BigInteger, equivalent to the result of getIntegerPart(). The second element is guaranteed to be a LongFraction, equivalent to the result of getFractionPart().

Equivalent to getParts(DivisionMode.TRUNCATED)

Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction. These represent the part that would be written before the decimal, and the part that would be after the decimal, if this fraction were written in decimal format.
See Also:
getParts(DivisionMode divisionMode)

getParts

public Number[] getParts(DivisionMode divisionMode)
Returns the integer and fraction parts of this fraction. The return array is guaranteed to have exactly two elements. The first is guaranteed to be a BigInteger, and the second is guaranteed to be a LongFraction.

The first element is the result of integer division of numerator by denominator, using the supplied division mode. The second element is the fraction given by numerator mod denominator, using the given division mode.

Note that the division mode only matters if this fraction is negative. Because the sign of a LongFraction is always carried by the numerator, the FLOOR and EUCLIDEAN division modes will always produce the same result.

Some examples:

FractionTRUNCATEFLOOREUCLIDEAN
4/1[ 4, 0/1][ 4, 0/1][ 4, 0/1]
4/3[ 1, 1/3][ 1, 1/3][ 1, 1/3]
2/7[ 0, 2/7][ 0, 2/7][ 0, 2/7]
-2/7[ 0, -2/7][-1, 5/7][-1, 5/7]
-4/3[-1, -1/3][-2, 2/3][-2, 2/3]
-4/1[-4, 0/1][-4, 0/1][-4, 0/1]

Parameters:
divisionMode - Division mode to use when computing parts. Only relevant if this is negative.
Returns:
Two Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a LongFraction. These represent the part that would be written before the decimal, and the part that would be after the decimal, if this fraction were written in decimal format.
See Also:
DivisionMode

round

public long round()
Returns this rounded to the nearest whole number, using RoundingMode.HALF_UP as the default rounding mode.

Returns:
this fraction rounded to nearest whole number, using RoundingMode.HALF_UP.

round

public long round(RoundingMode roundingMode)
Returns this fraction rounded to a whole number, using the given rounding mode.

Parameters:
roundingMode - rounding mode to use
Returns:
this fraction rounded to a whole number, using the given rounding mode.
Throws:
ArithmeticException - if RoundingMode.UNNECESSARY is used but this fraction does not exactly represent an integer.

roundToNumber

public LongFraction roundToNumber(Number n)
Rounds this fraction to the nearest multiple of the given number, using HALF_UP rounding method.

Parameters:
n - number to which we will round to the nearest multiple
Returns:
this value, rounded to the nearest multiple of n
Throws:
IllegalArgumentException - If n is null.
ArithmeticException - If n is zero or negative.

roundToNumber

public LongFraction roundToNumber(Number n,
                                  RoundingMode roundingMode)
Rounds this fraction to the nearest multiple of the given number, using the specified rounding method.

Note for HALF_EVEN rounding method: this rounds to the nearest even multiple of n, which may or may not be even. For example, if rounding to the nearest 2, every result will be even. So 9 rounded to nearest 2 with HALF_EVEN will round to 8, since 8=2*4 (4 being an even number), whereas 10=2*5 (5 being odd).

Parameters:
n - number to which we will round to the nearest multiple
roundingMode - rounding mode to use if the answer must be rounded
Returns:
this value, rounded to the nearest multiple of n
Throws:
IllegalArgumentException - If n is null.
ArithmeticException - If n is zero or negative.
ArithmeticException - if RoundingMode.UNNECESSARY is used but this fraction is not an exact multiple of the given value.

roundToDenominator

public long roundToDenominator(long newDenominator)
Rounds the given fraction to the nearest fraction having the given denominator, using HALF_UP rounding method, and returns the numerator of that fraction.

Parameters:
newDenominator - denominator of fraction to round to.
Returns:
numerator of rounded fraction (unreduced)
Throws:
IllegalArgumentException - If newDenominator is null.
ArithmeticException - If newDenominator is zero or negative.
See Also:
roundToDenominator(long, RoundingMode)

roundToDenominator

public long roundToDenominator(long newDenominator,
                               RoundingMode roundingMode)
Rounds the given fraction to the nearest fraction having the given denominator, using the given rounding method, and returns the numerator of that fraction.

For example, given the fraction 7/15, if you wanted to know the nearest fraction with denominator 6, it would be 2.8/6, which rounds to 3/6. This function would return 3.

Note: this is not reduced--3/6 is equivalent to 1/2, but this function would still return 3. If newDenominator is 1, this method is equivalent to round(). If this object is negative, the returned numerator will also be negative.

Parameters:
newDenominator - denominator of fraction to round to.
roundingMode - rounding mode to use if the answer must be rounded.
Returns:
numerator of rounded fraction (unreduced)
Throws:
ArithmeticException - If newDenominator is zero or negative.
ArithmeticException - if RoundingMode.UNNECESSARY is used but this fraction cannot be represented exactly as a fraction with the given denominator.

toString

public String toString()
Returns a string representation of this, in the form numerator/denominator. The denominator will always be included, even if it is 1.

Overrides:
toString in class Object
Returns:
This fraction, represented as a string in the format numerator/denominator.

toString

public String toString(int radix)
Returns string representation of this, in the form of numerator/denominator, with numerator and denominator represented in the given radix. The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
Returns:
This fraction, represented as a string in the format numerator/denominator.

toString

public String toString(boolean denominatorOptional)
Returns a string representation of this, in the form of numerator/denominator. Optionally, "/denominator" part can be ommitted for whole numbers.

Parameters:
denominatorOptional - If true, the denominator will be ommitted when it is unnecessary. For example, "7" instead of "7/1".
Returns:
This fraction, represented as a string in the format numerator/denominator.

toString

public String toString(int radix,
                       boolean denominatorOptional)
Returns string representation of this, in the form of numerator/denominator, with numerator and denominator represented in the given radix. The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Optionally, "/denominator" part can be ommitted for whole numbers.

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
denominatorOptional - If true, the denominator will be ommitted when it is unnecessary. For example, "7" instead of "7/1".
Returns:
This fraction, represented as a string in the format numerator/denominator.

toMixedString

public String toMixedString()
Returns string representation of this object as a mixed fraction. For example, 4/3 would be "1 1/3". For negative fractions, the sign is carried only by the whole number and assumed to be distributed across the whole value. For example, -4/3 would be "-1 1/3". For fractions that are equal to whole numbers, only the whole number will be displayed. For fractions which have absolute value less than 1, this will be equivalent to toString().

Returns:
String representation of this fraction as a mixed fraction.

toMixedString

public String toMixedString(int radix)
Returns string representation of this object as a mixed fraction. For example, 4/3 would be "1 1/3". For negative fractions, the sign is carried only by the whole number and assumed to be distributed across the whole value. For example, -4/3 would be "-1 1/3". For fractions that are equal to whole numbers, only the whole number will be displayed. For fractions which have absolute value less than 1, this will be equivalent to toString(int radix).

The numbers are represented in the given radix. The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
Returns:
String representation of this fraction as a mixed fraction.

toDecimalString

public String toDecimalString(int numDecimalDigits)
Returns decimal string representation of the fraction with the given number of decimal digits using roundingMode ROUND_HALF_UP.

Parameters:
numDecimalDigits - number of digits to be displayed after the decimal
Returns:
decimal string representation of this fraction.
Throws:
ArithmeticException - if roundingMode is UNNECESSARY but rounding is required.

toDecimalString

public String toDecimalString(int numDecimalDigits,
                              RoundingMode roundingMode)
Converts the fraction to a string with the given number of decimal digits. For example, if f is 1/3, f.toDecimalString(1): 0.3; f.toDecimalString(4): 0.3333. If numDecimalDigits is 0, this method is equivalent to round().toString(). Will append trailing 0s as needed: (1/2).toDecimalString(3) is 0.500.

Parameters:
numDecimalDigits - number of digits to be displayed after the decimal
roundingMode - how to round the number if necessary
Returns:
decimal string representation of this fraction.
Throws:
ArithmeticException - if roundingMode is UNNECESSARY but rounding is required.

toRadixedString

public String toRadixedString(int radix,
                              int numFractionalDigits)
Converts the fraction to a radixed string with the given number of fraction digits after the radix point. Rounds using HALF_UP rounding mode. The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
numFractionalDigits - number of digits to be displayed after the radix point.
Returns:
radixed string representation of this fraction.

toRadixedString

public String toRadixedString(int radix,
                              int numFractionalDigits,
                              RoundingMode roundingMode)
Converts the fraction to a radixed string with the given number of fraction digits after the radix point.

For example, 1/8 in base 10 is 0.125. In base 2 it is 0.001.

Will append trailing 0s as needed: (1/2).toRadixedString(3,2) is 0.100.

If passed negative numFractionalDigits, rounds to nearest radix^(-numFractionalDigits). For example, -1 means round to nearest 10, -2 means round to nearest 100, etc. No extra zeros are prepended in this case, since the only time it would be necessary is if a value were rounded to zero.

The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
numFractionalDigits - number of digits to be displayed after the decimal
roundingMode - how to round the number if necessary
Returns:
radixed string representation of this fraction.
Throws:
ArithmeticException - if roundingMode is UNNECESSARY but rounding is required.

toRepeatingDigitString

public String toRepeatingDigitString()
Converts the fraction to a radixed string with repeating digits. The repeating digits are indicated by parenthesis: 1/9 becomes 0.(1)

Equivalent to toRepeatingString(10, false)

Returns:
radixed string representation of this fraction with repeating digits denoted in parenthesis.
See Also:
toRepeatingDigitString(int, boolean)

toRepeatingDigitString

public String toRepeatingDigitString(boolean forceRepeating)
Converts the fraction to a radixed string with repeating digits. The repeating digits are indicated by parenthesis: 1/9 becomes 0.(1)

Equivalent to toRepeatingString(10, forceRepeating)

Parameters:
forceRepeating - whether or not to force this function to always use a repeating fraction, even if the radixed string terminates
Returns:
radixed string representation of this fraction with repeating digits denoted in parenthesis.
See Also:
toRepeatingDigitString(int, boolean)

toRepeatingDigitString

public String toRepeatingDigitString(int radix)
Converts the fraction to a radixed string with repeating digits, in the given radix. The repeating digits are indicated by parenthesis: 1/9 becomes 0.(1)

Equivalent to toRepeatingString(radix, false)

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
Returns:
radixed string representation of this fraction with repeating digits denoted in parenthesis.
See Also:
toRepeatingDigitString(int, boolean)

toRepeatingDigitString

public String toRepeatingDigitString(int radix,
                                     boolean forceRepeating)
Converts the fraction to a radixed string with repeating digits, in the given radix. The repeating digits are indicated by parenthesis: 1/9 becomes 0.(1)

All rational fractions can be represented as a radixed string with repeating digits, but some fractions can also be represented as a radixed string that terminates. In these cases, the forceRepeating parameter can be used to force this function to return the repeating fraction. For example, the fraction 1/10 could be represented as terminating string "0.1" or as repeating string "0.0(9)".

There is one special case for the value of 0. If forceRepeating==true, the return value will be "0.(0)". For all other fractions, the repeating digits will never be all zeros.

The repeating digits will always follow the radix point. For example, 500/11 is represented as "45.(45)".

Warning: This method is quite slow, as it essentially implements long division.

The digit-to-character mapping provided by Character.forDigit(int, int) is used.

Examples:
LongFraction.valueOf(1,9).toRepeatingDigitString(10, false): 0.(1)
LongFraction.valueOf(1).toRepeatingDigitString(10, false): 1.0
LongFraction.valueOf(1).toRepeatingDigitString(10, true): 0.(9)
LongFraction.valueOf(1,100).toRepeatingDigitString(10, false): 0.01
LongFraction.valueOf(1,100).toRepeatingDigitString(10, true): 0.00(9)
LongFraction.valueOf(45,22).toRepeatingDigitString(10, false): 2.0(45)
LongFraction.valueOf(500,11).toRepeatingDigitString(10, false): 45.(45)

Parameters:
radix - radix of the String representation. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString)
forceRepeating - whether or not to force this function to always use a repeating fraction, even if the radixed string terminates
Returns:
radixed string representation of this fraction with repeating digits denoted in parenthesis.

equals

public boolean equals(Object o)
Returns if this object is equal to another object. In order to maintain symmetry, this will *only* return true if the other object is a LongFraction. For looser comparison to other Number objects, use the equalsNumber(Number) method.

Overrides:
equals in class Object
Parameters:
o - Object to compare to this
Returns:
true if o instanceof LongFraction and is equal to this.
See Also:
equalsNumber(Number)

equalsNumber

public boolean equalsNumber(Number n)
Returns if this object is equal to another Number object. Equivalent to: this.equals(LongFraction.valueOf(n))

Parameters:
n - number to compare this to
Returns:
true if this is equivalent to valueof(n)

hashCode

public int hashCode()
Returns a hash code for this object.

Overrides:
hashCode in class Object
Returns:
hash code for this object.

compareTo

public int compareTo(Number n)
Returns a negative, zero, or positive number, indicating if this object is less than, equal to, or greater than n, respectively.

Specified by:
compareTo in interface Comparable<Number>
Parameters:
n - number to compare this to
Returns:
integer indicating how this compares to given number
Throws:
IllegalArgumentException - if n is null

fareyNext

public LongFraction fareyNext(int maxDenominator)
Returns the next fraction in the Farey sequence with denominator less than or equal to the given denominator. This is the smallest fraction that is larger than this, with a denominator less than or equal to maxDenominator. Algorithm is O(maxDenominator), and not optimized for generating entire sequence by sequentially calling this function.

Parameters:
maxDenominator - maximum denominator to use for computing next value in Farey sequence.
Returns:
the smallest fraction that is larger than this, with a denominator less than or equal to maxDenominator.
Throws:
IllegalArgumentException - if maxDenominator is non-positive.

fareyPrev

public LongFraction fareyPrev(int maxDenominator)
Returns the previous fraction in the Farey sequence with denominator less than or equal to the given denominator. This is the largest fraction that is smaller than this, with a denominator less than or equal to maxDenominator. Algorithm is O(maxDenominator), and not optimized for generating entire sequence by sequentially calling this function.

Parameters:
maxDenominator - maximum denominator to use for computing previous value in Farey sequence.
Returns:
the largest fraction that is smaller than this, with a denominator less than or equal to maxDenominator.
Throws:
IllegalArgumentException - if maxDenominator is non-positive.

fareyClosest

public LongFraction fareyClosest(int maxDenominator)
Returns the closest fraction with denominator less than or equal to the given denominator. Algorithm is O(maxDenominator).

Parameters:
maxDenominator - maximum denominator to use for computing closest value in Farey sequence.
Returns:
the closest fraction that to this, with a denominator less than or equal to maxDenominator.
Throws:
IllegalArgumentException - if maxDenominator is non-positive.

min

public Number min(Number n)
Returns the smaller of this and n. If they have equal value, this is returned. Worth noting: if n is smaller, the returned Number is n, not a LongFraction representing n.

Parameters:
n - number to compare to this
Returns:
smaller of this and n
Throws:
IllegalArgumentException - if n is null

min

public static Number min(Number a,
                         Number b)
Returns the smaller of a and b. If they have equal value, a is returned. Worth noting: the returned Number is always one of the two arguments, not necessarily a LongFraction.

Parameters:
a - one number to compare
b - another number to compare
Returns:
smaller of a and b
Throws:
IllegalArgumentException - if a or b is null

max

public Number max(Number n)
Returns the larger of this and n. If they have equal value, this is returned. Worth noting: if n is larger, the returned Number is n, not a LongFraction representing n.

Parameters:
n - number to compare to this
Returns:
larger of this and n
Throws:
IllegalArgumentException - if n is null

max

public static Number max(Number a,
                         Number b)
Returns the larger of a and b. If they have equal value, a is returned. Worth noting: the returned Number is always one of the two arguments, not necessarily a LongFraction.

Parameters:
a - one number to compare
b - another number to compare
Returns:
larger of a and b
Throws:
IllegalArgumentException - if a or b is null

mediant

public LongFraction mediant(Number n)
Returns the mediant of this and n. The mediant of a/b and c/d is (a+c)/(b+d). It is guaranteed to be between a/b and c/d. Not to be confused with the median!

Parameters:
n - other number to use to compute mediant
Returns:
mediant of this and n
Throws:
IllegalArgumentException - if n is null

mediant

public static LongFraction mediant(Number a,
                                   Number b)
Returns the mediant of a and b. Provided as static method for convenience.

Parameters:
a - one number to use to compute mediant
b - other number to use to compute mediant
Returns:
mediant of a and b
Throws:
IllegalArgumentException - if a or b is null
See Also:
mediant(Number)

toBigDecimal

public BigDecimal toBigDecimal()
Returns a BigDecimal representation of this fraction.

If possible, the returned value will be exactly equal to the fraction. If not, this is equivalent to toBigDecimal(18), approximately the same precision as a double-precision number.

Returns:
This fraction represented as a BigDecimal (exactly, if possible).

toBigDecimal

public BigDecimal toBigDecimal(int precision)
Returns a BigDecimal representation of this fraction, with a given precision.

Parameters:
precision - the number of significant figures to be used in the result.
Returns:
BigDecimal representation of this fraction, to the given precision.

longValue

public long longValue()
Returns a long representation of this fraction. This value is obtained by integer division of numerator by denominator.

Specified by:
longValue in class Number
Returns:
long representation of this fraction

longValueExact

public long longValueExact()
Returns an exact long representation of this fraction.

Returns:
exact long representation of this fraction
Throws:
ArithmeticException - if this has a nonzero fractional part, or will not fit in a long.

intValue

public int intValue()
Returns an int representation of this fraction. This value is obtained by integer division of numerator by denominator. If the value is greater than Integer.MAX_VALUE, Integer.MAX_VALUE will be returned. Similarly, if the value is below Integer.MIN_VALUE, Integer.MIN_VALUE will be returned.

Specified by:
intValue in class Number
Returns:
int representation of this fraction

intValueExact

public int intValueExact()
Returns an exact int representation of this fraction.

Returns:
exact int representation of this fraction
Throws:
ArithmeticException - if this has a nonzero fractional part, or will not fit in an int.

shortValue

public short shortValue()
Returns a short representation of this fraction. This value is obtained by integer division of numerator by denominator. If the value is greater than Short.MAX_VALUE, Short.MAX_VALUE will be returned. Similarly, if the value is below Short.MIN_VALUE, Short.MIN_VALUE will be returned.

Overrides:
shortValue in class Number
Returns:
short representation of this fraction

shortValueExact

public short shortValueExact()
Returns an exact short representation of this fraction.

Returns:
exact short representation of this fraction
Throws:
ArithmeticException - if this has a nonzero fractional part, or will not fit in a short.

byteValue

public byte byteValue()
Returns a byte representation of this fraction. This value is obtained by integer division of numerator by denominator. If the value is greater than Byte.MAX_VALUE, Byte.MAX_VALUE will be returned. Similarly, if the value is below Byte.MIN_VALUE, Byte.MIN_VALUE will be returned.

Overrides:
byteValue in class Number
Returns:
byte representation of this fraction

byteValueExact

public byte byteValueExact()
Returns an exact byte representation of this fraction.

Returns:
exact byte representation of this fraction
Throws:
ArithmeticException - if this has a nonzero fractional part, or will not fit in a byte.

doubleValue

public double doubleValue()
Returns the value of this fraction. If this value is beyond the range of a double, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY will be returned.

Specified by:
doubleValue in class Number
Returns:
double representation of this fraction

doubleValueExact

public double doubleValueExact()
Returns an exact double representation of this fraction.

Warning: Current algorithm is simply to convert to double, then convert back to BigFraction, then make sure the copy of copy is identical to the original. This is not optimized, and fails on some edge cases. This will be addressed in future updates.

Returns:
exact double representation of this fraction
Throws:
ArithmeticException - if this cannot be represented exactly as a double.

floatValue

public float floatValue()
Returns the value of this fraction. If this value is beyond the range of a float, Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY will be returned.

Specified by:
floatValue in class Number
Returns:
float representation of this fraction

floatValueExact

public float floatValueExact()
Returns an exact float representation of this fraction.

Warning: Current algorithm is simply to convert to float, then convert back to BigFraction, then make sure the copy of copy is identical to the original. This is not optimized, and fails on some edge cases. This will be addressed in future updates.

Returns:
exact float representation of this fraction
Throws:
ArithmeticException - if this cannot be represented exactly as a float.