public final class BigFraction extends Number implements Comparable<Number>
| Modifier and Type | Field and Description |
|---|---|
static BigFraction |
ONE
The value 1/1.
|
static BigFraction |
ONE_HALF
The value 1/2.
|
static BigFraction |
ONE_TENTH
The value 1/10.
|
static BigFraction |
TEN
The value 10/1.
|
static BigFraction |
ZERO
The value 0/1.
|
| Constructor and Description |
|---|
BigFraction(Number n)
Note:
valueOf(Number) should be preferred for performance reasons. |
BigFraction(Number numerator,
Number denominator)
Note:
valueOf(Number, Number) should be preferred for performance reasons. |
BigFraction(String s)
Note:
valueOf(String) should be preferred for performance reasons. |
BigFraction(String s,
int radix)
Note:
valueOf(String, int) should be preferred for performance reasons. |
| Modifier and Type | Method and Description |
|---|---|
BigFraction |
abs()
Returns the absolute value of this.
|
BigFraction |
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.
|
BigFraction |
complement()
Returns the complement of this fraction, which is equal to 1 - this.
|
static BigFraction |
difference(Number a,
Number b)
Returns a - b, represented as a BigFraction.
|
BigFraction |
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.
|
BigFraction |
divideInto(Number n)
Returns n / this.
|
BigInteger |
divideToIntegralValue(Number n)
Divides to an integral value, using
DivisionMode.TRUNCATED division mode. |
BigInteger |
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.
|
BigFraction |
fareyClosest(int maxDenominator)
Returns the closest fraction with denominator less than or equal to
the given denominator.
|
BigFraction |
fareyNext(int maxDenominator)
Returns the next fraction in the Farey sequence with denominator less than
or equal to the given denominator.
|
BigFraction |
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. |
BigFraction |
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, ... |
BigInteger |
getDenominator()
Returns the denominator of this fraction.
|
BigFraction |
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.
|
BigFraction |
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.
|
BigInteger |
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.
|
BigInteger |
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.
|
BigInteger |
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 BigInteger |
integralQuotient(Number a,
Number b)
Returns result of integer division a / b, using truncated division mode.
|
static BigInteger |
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.
|
BigFraction |
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.
|
BigFraction |
mediant(Number n)
Returns the mediant of this and n.
|
static BigFraction |
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.
|
BigFraction |
multiply(Number n)
Returns this * n.
|
BigFraction |
negate()
Returns -this.
|
BigFraction |
pow(int exponent)
Returns this^exponent.
Note: 0^0 will return 1/1. |
static BigFraction |
product(Number a,
Number b)
Returns a * b, represented as a BigFraction.
|
static BigFraction |
quotient(Number a,
Number b)
Returns a / b, represented as a BigFraction.
|
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.
|
BigFraction |
reciprocal()
Returns 1/this.
|
BigFraction |
remainder(Number n)
Computes division remainder (modulus), using
DivisionMode.TRUNCATED division mode. |
BigFraction |
remainder(Number n,
DivisionMode divisionMode)
Computes division remainder (modulus), using the specified division mode.
|
static BigFraction |
remainder(Number a,
Number b)
Returns fractional remainder of integer division a / b, using truncated division mode.
|
static BigFraction |
remainder(Number a,
Number b,
DivisionMode divisionMode)
Returns fractional remainder of integer division a / b, using specified division mode.
|
BigInteger |
round()
Returns this rounded to the nearest whole number, using
RoundingMode.HALF_UP as the default rounding mode.
|
BigInteger |
round(RoundingMode roundingMode)
Returns this fraction rounded to a whole number, using
the given rounding mode.
|
BigInteger |
roundToDenominator(BigInteger 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.
|
BigInteger |
roundToDenominator(BigInteger 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. |
BigFraction |
roundToNumber(Number n)
Rounds this fraction to the nearest multiple of the given number, using HALF_UP
rounding method.
|
BigFraction |
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.
|
BigFraction |
subtract(Number n)
Returns this - n.
|
BigFraction |
subtractFrom(Number n)
Returns n - this.
|
static BigFraction |
sum(Number a,
Number b)
Returns a + b, represented as a BigFraction.
|
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 BigFraction |
valueOf(Number n)
Constructs a BigFraction from given number.
|
static BigFraction |
valueOf(Number numerator,
Number denominator)
Constructs a BigFraction with given numerator and denominator.
|
static BigFraction |
valueOf(String s)
Constructs a BigFraction from a String.
|
static BigFraction |
valueOf(String s,
int radix)
Constructs a BigFraction from a String.
|
BigFraction |
withSign(int sgn)
Returns this, with sign set to the sign of
sgn parameter. |
public static final BigFraction ZERO
public static final BigFraction ONE
public static final BigFraction ONE_HALF
public static final BigFraction ONE_TENTH
public static final BigFraction TEN
public BigFraction(Number n)
valueOf(Number) should be preferred for performance reasons.
This constructor is provided for convenience.n - Number to convert to BigFraction.valueOf(Number)public BigFraction(Number numerator, Number denominator)
valueOf(Number, Number) should be preferred for performance reasons.
This constructor is provided for convenience.numerator - numerator of new BigFractiondenominator - denominator of new BigFractionvalueOf(Number, Number)public BigFraction(String s)
valueOf(String) should be preferred for performance reasons.
This constructor is provided for convenience.s - String to convert to parse as BigFractionvalueOf(String)public BigFraction(String s, int radix)
valueOf(String, int) should be preferred for performance reasons.
This constructor is provided for convenience.s - String to convert to parse as BigFractionradix - 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)valueOf(String, int)public static BigFraction valueOf(Number n)
Number.doubleValue()
will be used for construction.System.out.println(BigFraction.valueOf(1.1))2476979795053773/22517998136852481.1, is:
(-1)^0 * 2^0 * (1 + 0x199999999999aL / 0x10000000000000L).)BigFraction.valueOf(Double.toString(d)) may give the result
the user expects.n - Any Number to be converted to a BigFractionn. Guaranteed to be non-null.IllegalArgumentException - if n is null.public static BigFraction valueOf(Number numerator, Number denominator)
BigFraction.valueOf(8.5, -6.25); //-34/25System.out.println(BigFraction.valueOf(1.1))1.1, is:
(-1)^0 * 2^0 * (1 + 0x199999999999aL / 0x10000000000000L).)BigFraction.valueOf(Double.toString(d)) may give the result
the user expects.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.numerator/denominator. Guaranteed to be non-null.ArithmeticException - if denominator == 0.IllegalArgumentException - if numerator or denominator is null.public static BigFraction valueOf(String s)
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.BigFraction.valueOf("11"); //11/1BigFraction.valueOf("22/34"); //11/17BigFraction.valueOf("2e4/-0.64"); //-174375/4BigFraction.valueOf("0.(1)"); //1/9BigFraction.valueOf("12.34(56)"); //122222/9900s - a string representation of a number or fractionNumberFormatException - if the string cannot be properly parsed.ArithmeticException - if denominator == 0.IllegalArgumentException - if s is null.BigDecimal.BigDecimal(String)public static BigFraction valueOf(String s, int radix)
numerator/denominator,
but "/denominator" part is optional.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.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.BigFraction.valueOf("11", 10); //11/1BigFraction.valueOf("22/34", 10); //11/17BigFraction.valueOf("2e4/-0.64", 10); //-174375/4BigFraction.valueOf("dead/beef", 16); //57005/48879BigFraction.valueOf("lazy.fox", 36); //15459161339/15552BigFraction.valueOf("0.(1)", 10); //1/9BigFraction.valueOf("12.34(56)", 10); //122222/9900BigFraction.valueOf("0.(1)", 16); //1/15BigFraction.valueOf("the.lazy(fox)", 36); //2994276908470787/78362484480s - a string representation of a number or fractionradix - 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)NumberFormatException - if the string cannot be properly parsed.ArithmeticException - if denominator == 0.IllegalArgumentException - if s is null.BigDecimal.BigDecimal(String),
BigInteger.BigInteger(String, int)public final BigInteger getNumerator()
public final BigInteger getDenominator()
public BigFraction add(Number n)
n - number to be added to thisIllegalArgumentException - if n is null.public static BigFraction sum(Number a, Number b)
BigFraction.valueOf(a).add(b).
Provided as static method to make code easier to write in some instances.a - number to be addedb - number to be addedIllegalArgumentException - if a or b is null.public BigFraction subtract(Number n)
n - number to be subtracted from thisIllegalArgumentException - if n is null.public BigFraction subtractFrom(Number n)
n - number to subtract this fromIllegalArgumentException - if n is null.public static BigFraction difference(Number a, Number b)
BigFraction.valueOf(a).subtract(b).
Provided as static method to make code easier to write in some instances.a - number to subtract from (minuend)b - number to be subtracted from a (subtrahend)IllegalArgumentException - if a or b is null.public BigFraction multiply(Number n)
n - number to be multiplied by thisIllegalArgumentException - if n is null.public static BigFraction product(Number a, Number b)
BigFraction.valueOf(a).multiply(b).
Provided as static method to make code easier to write in some instances.a - number to be multipliedb - number to be multipliedIllegalArgumentException - if a or b is null.public BigFraction divide(Number n)
n - number to divide this by (divisor)IllegalArgumentException - if n is null.ArithmeticException - if n == 0.public BigFraction divideInto(Number n)
n - number to be divided by this (dividend)IllegalArgumentException - if n is null.ArithmeticException - if this == 0.public static BigFraction quotient(Number a, Number b)
BigFraction.valueOf(a).divide(b).
Also equivalent to BigFraction.valueOf(a, b).
Provided as static method to make code easier to write in some instances.a - number to be divided (dividend)b - number by which to divide (divisor)IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.public BigInteger divideToIntegralValue(Number n)
DivisionMode.TRUNCATED division mode.n - number to divide this by (dividend)IllegalArgumentException - if n is null.ArithmeticException - if n == 0.divideAndRemainder(Number, DivisionMode),
DivisionModepublic BigInteger divideToIntegralValue(Number n, DivisionMode divisionMode)
n - number to divide this by (dividend)divisionMode - division mode to use if dividend or divisor is negative.IllegalArgumentException - if n is null.ArithmeticException - if n == 0.divideAndRemainder(Number, DivisionMode),
DivisionModepublic BigFraction remainder(Number n)
DivisionMode.TRUNCATED division mode.n - number to divide this by (dividend)IllegalArgumentException - if n is null.ArithmeticException - if n == 0.divideAndRemainder(Number, DivisionMode),
DivisionModepublic BigFraction remainder(Number n, DivisionMode divisionMode)
n - number to divide this by (dividend)divisionMode - division mode to use if dividend or divisor is negative.IllegalArgumentException - if n is null.ArithmeticException - if n == 0.divideAndRemainder(Number, DivisionMode),
DivisionModepublic Number[] divideAndRemainder(Number n)
DivisionMode.TRUNCATED division mode.n - number to divide this by (dividend)Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.IllegalArgumentException - if n is null.ArithmeticException - if n == 0.divideAndRemainder(Number, DivisionMode),
DivisionModepublic Number[] divideAndRemainder(Number n, DivisionMode divisionMode)
this/n = q + r/nthis = q*n + rn - Number to divide this by.divisionMode - Division mode to use if dividend or divisor is negative.Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.IllegalArgumentException - if n is null.ArithmeticException - if n == 0.DivisionModepublic static BigInteger integralQuotient(Number a, Number b)
BigFraction.valueOf(a).divideToIntegralValue(b).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.divideToIntegralValue(Number n),
DivisionModepublic static BigInteger integralQuotient(Number a, Number b, DivisionMode divisionMode)
BigFraction.valueOf(a).divideToIntegralValue(b, divisionMode).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)divisionMode - division mode to use when dividend or divisor is negative.IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.divideToIntegralValue(Number n, DivisionMode divisionMode),
DivisionModepublic static BigFraction remainder(Number a, Number b)
BigFraction.valueOf(a).remainder(b).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.remainder(Number n),
DivisionModepublic static BigFraction remainder(Number a, Number b, DivisionMode divisionMode)
BigFraction.valueOf(a).remainder(b, divisionMode).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)divisionMode - division mode to use when dividend or divisor is negative.IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.remainder(Number n, DivisionMode divisionMode),
DivisionModepublic static Number[] quotientAndRemainder(Number a, Number b)
BigFraction.valueOf(a).divideAndRemainder(b).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.divideAndRemainder(Number n),
DivisionModepublic static Number[] quotientAndRemainder(Number a, Number b, DivisionMode divisionMode)
BigFraction.valueOf(a).divideAndRemainder(b, divisionMode).
Provided as static method to make code easier to write in some instances.a - number be divided (dividend)b - number to divide by (divisor)divisionMode - division mode to use when dividend or divisor is negative.Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.IllegalArgumentException - if a or b is null.ArithmeticException - if b == 0.divideAndRemainder(Number n, DivisionMode divisionMode),
DivisionModepublic BigFraction gcd(Number n)
this and n.this and n are both zero, returns 0/1.n - other value to compute gcd from.this and n.public BigFraction lcm(Number n)
this and n.this or n is zero, returns 0/1.n - other value to compute lcm from.this and npublic BigFraction pow(int exponent)
Math.pow(double, double),
BigInteger.pow(int), and BigDecimal.pow(int).exponent - power to raise this fraction to.ArithmeticException - if this == 0 && exponent < 0.public BigFraction reciprocal()
ArithmeticException - if this == 0.public BigFraction complement()
public BigFraction negate()
this.multiply(-1)public BigFraction abs()
public BigFraction withSign(int sgn)
sgn parameter.
Another way of saying it: returns the equivalent of this.abs().multiply(Math.signum(sgn)).sgn - an integer less than, equal to, or greater than 0, whose sign will be assigned to the returned fraction.this.abs().multiply(Math.signum(sgn)).public int signum()
public BigInteger getIntegerPart()
getIntegerPart(DivisionMode.TRUNCATED)getParts(DivisionMode divisionMode)public BigInteger getIntegerPart(DivisionMode divisionMode)
divisionMode - Division mode to use when computing quotient. Only relevant if this is negative.getParts(DivisionMode divisionMode)public BigFraction getFractionPart()
getFractionPart(DivisionMode.TRUNCATED)(numerator%denominator)/denominator)), using TRUNCATED division mode.getParts(DivisionMode divisionMode)public BigFraction getFractionPart(DivisionMode divisionMode)
divisionMode - Division mode to use when computing remainder. Only relevant if this is negative.(numerator%denominator)/denominator)), using specified division mode.getParts(DivisionMode divisionMode)public Number[] getParts()
getParts(DivisionMode.TRUNCATED)Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.
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.getParts(DivisionMode divisionMode)public Number[] getParts(DivisionMode divisionMode)
| Fraction | TRUNCATE | FLOOR | EUCLIDEAN |
|---|---|---|---|
| 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] |
divisionMode - Division mode to use when computing parts. Only relevant if this is negative.Number objects. Guaranteed to be two non-null elements. First is a BigInteger, second is a BigFraction.
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.DivisionModepublic BigInteger round()
public BigInteger round(RoundingMode roundingMode)
roundingMode - rounding mode to useArithmeticException - if RoundingMode.UNNECESSARY is used but
this fraction does not exactly represent an integer.public BigFraction roundToNumber(Number n)
n - number to which we will round to the nearest multipleIllegalArgumentException - If n is null.ArithmeticException - If n is zero or negative.public BigFraction roundToNumber(Number n, RoundingMode roundingMode)
n - number to which we will round to the nearest multipleroundingMode - rounding mode to use if the answer must be roundedIllegalArgumentException - 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.public BigInteger roundToDenominator(BigInteger newDenominator)
newDenominator - denominator of fraction to round to.IllegalArgumentException - If newDenominator is null.ArithmeticException - If newDenominator is zero or negative.roundToDenominator(BigInteger, RoundingMode)public BigInteger roundToDenominator(BigInteger newDenominator, RoundingMode roundingMode)
newDenominator - denominator of fraction to round to.roundingMode - rounding mode to use if the answer must be rounded.IllegalArgumentException - If newDenominator is null.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.public String toString()
public String toString(int radix)
Character.forDigit(int, int) is used.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)numerator/denominator.public String toString(boolean denominatorOptional)
denominatorOptional - If true, the denominator will be ommitted
when it is unnecessary. For example, "7" instead of "7/1".numerator/denominator.public String toString(int radix, boolean denominatorOptional)
Character.forDigit(int, int) is used.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".numerator/denominator.public String toMixedString()
toString().public String toMixedString(int radix)
toString(int radix).Character.forDigit(int, int) is used.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)public String toDecimalString(int numDecimalDigits)
numDecimalDigits - number of digits to be displayed after the decimalArithmeticException - if roundingMode is UNNECESSARY but rounding is required.public String toDecimalString(int numDecimalDigits, RoundingMode roundingMode)
numDecimalDigits - number of digits to be displayed after the decimalroundingMode - how to round the number if necessaryArithmeticException - if roundingMode is UNNECESSARY but rounding is required.public String toRadixedString(int radix, int numFractionalDigits)
Character.forDigit(int, int) is used.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.public String toRadixedString(int radix, int numFractionalDigits, RoundingMode roundingMode)
Character.forDigit(int, int) is used.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 decimalroundingMode - how to round the number if necessaryArithmeticException - if roundingMode is UNNECESSARY but rounding is required.public String toRepeatingDigitString()
toRepeatingString(10, false)toRepeatingDigitString(int, boolean)public String toRepeatingDigitString(boolean forceRepeating)
toRepeatingString(10, forceRepeating)forceRepeating - whether or not to force this function to always use a repeating fraction,
even if the radixed string terminatestoRepeatingDigitString(int, boolean)public String toRepeatingDigitString(int radix)
toRepeatingString(radix, false)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)toRepeatingDigitString(int, boolean)public String toRepeatingDigitString(int radix, boolean forceRepeating)
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)".forceRepeating==true, the return
value will be "0.(0)". For all other fractions, the repeating digits will never
be all zeros.500/11 is
represented as "45.(45)".Character.forDigit(int, int) is used.BigFraction.valueOf(1,9).toRepeatingDigitString(10, false): 0.(1)BigFraction.valueOf(1).toRepeatingDigitString(10, false): 1.0BigFraction.valueOf(1).toRepeatingDigitString(10, true): 0.(9)BigFraction.valueOf(1,100).toRepeatingDigitString(10, false): 0.01BigFraction.valueOf(1,100).toRepeatingDigitString(10, true): 0.00(9)BigFraction.valueOf(45,22).toRepeatingDigitString(10, false): 2.0(45)BigFraction.valueOf(500,11).toRepeatingDigitString(10, false): 45.(45)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 terminatespublic boolean equals(Object o)
equals in class Objecto - Object to compare to thiso instanceof BigFraction and is equal to this.equalsNumber(Number)public boolean equalsNumber(Number n)
this.equals(BigFraction.valueOf(n))n - number to compare this tovalueof(n)public int hashCode()
public int compareTo(Number n)
compareTo in interface Comparable<Number>n - number to compare this toIllegalArgumentException - if n is nullpublic BigFraction fareyNext(int maxDenominator)
maxDenominator - maximum denominator to use for computing next value
in Farey sequence.IllegalArgumentException - if maxDenominator is non-positive.public BigFraction fareyPrev(int maxDenominator)
maxDenominator - maximum denominator to use for computing previous value
in Farey sequence.IllegalArgumentException - if maxDenominator is non-positive.public BigFraction fareyClosest(int maxDenominator)
maxDenominator - maximum denominator to use for computing closest value
in Farey sequence.IllegalArgumentException - if maxDenominator is non-positive.public Number min(Number n)
n - number to compare to thisIllegalArgumentException - if n is nullpublic static Number min(Number a, Number b)
a - one number to compareb - another number to compareIllegalArgumentException - if a or b is nullpublic Number max(Number n)
n - number to compare to thisIllegalArgumentException - if n is nullpublic static Number max(Number a, Number b)
a - one number to compareb - another number to compareIllegalArgumentException - if a or b is nullpublic BigFraction mediant(Number n)
n - other number to use to compute mediantIllegalArgumentException - if n is nullpublic static BigFraction mediant(Number a, Number b)
a - one number to use to compute mediantb - other number to use to compute mediantIllegalArgumentException - if a or b is nullmediant(Number)public BigDecimal toBigDecimal()
toBigDecimal(18), approximately the same precision
as a double-precision number.public BigDecimal toBigDecimal(int precision)
precision - the number of significant figures to be used in the result.public long longValue()
Long.MAX_VALUE, Long.MAX_VALUE will be
returned. Similarly, if the value is below Long.MIN_VALUE,
Long.MIN_VALUE will be returned.public long longValueExact()
ArithmeticException - if this has a nonzero fractional
part, or will not fit in a long.public int intValue()
Integer.MAX_VALUE, Integer.MAX_VALUE will be
returned. Similarly, if the value is below Integer.MIN_VALUE,
Integer.MIN_VALUE will be returned.public int intValueExact()
ArithmeticException - if this has a nonzero fractional
part, or will not fit in an int.public short shortValue()
Short.MAX_VALUE, Short.MAX_VALUE will be
returned. Similarly, if the value is below Short.MIN_VALUE,
Short.MIN_VALUE will be returned.shortValue in class Numberpublic short shortValueExact()
ArithmeticException - if this has a nonzero fractional
part, or will not fit in a short.public byte byteValue()
Byte.MAX_VALUE, Byte.MAX_VALUE will be
returned. Similarly, if the value is below Byte.MIN_VALUE,
Byte.MIN_VALUE will be returned.public byte byteValueExact()
ArithmeticException - if this has a nonzero fractional
part, or will not fit in a byte.public double doubleValue()
Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY will
be returned.doubleValue in class Numberpublic double doubleValueExact()
ArithmeticException - if this cannot be represented exactly as a double.public float floatValue()
Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY will
be returned.floatValue in class Numberpublic float floatValueExact()
ArithmeticException - if this cannot be represented exactly as a float.