Class Fraction
- java.lang.Object
-
- java.lang.Number
-
- org.apache.commons.numbers.fraction.Fraction
-
- All Implemented Interfaces:
Serializable
,Comparable<Fraction>
,Addition<Fraction>
,Multiplication<Fraction>
,NativeOperators<Fraction>
public final class Fraction extends Number implements Comparable<Fraction>, NativeOperators<Fraction>, Serializable
Representation of a rational number.The number is expressed as the quotient
p/q
of two 32-bit integers, a numeratorp
and a non-zero denominatorq
.This class is immutable. Rational number
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Fraction
abs()
Returns the absolute value of this fraction.Fraction
add(int value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.Fraction
add(Fraction value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.int
compareTo(Fraction other)
Compares this object with the specified object for order using the signed magnitude.Fraction
divide(int value)
Divide this fraction by the passedvalue
, returning the result in reduced form.Fraction
divide(Fraction value)
Divide this fraction by the passedvalue
, returning the result in reduced form.double
doubleValue()
Returns thedouble
value closest to this fraction.boolean
equals(Object other)
Test for equality with another object.float
floatValue()
Returns thefloat
value closest to this fraction.static Fraction
from(double value)
Create a fraction given the double value.static Fraction
from(double value, double epsilon, int maxIterations)
Create a fraction given the double value and maximum error allowed.static Fraction
from(double value, int maxDenominator)
Create a fraction given the double value and maximum denominator.int
getDenominator()
Access the denominator as anint
.int
getNumerator()
Access the numerator as anint
.int
hashCode()
int
intValue()
Returns the whole number part of the fraction.boolean
isOne()
Check if this is a neutral element of multiplication, i.e.boolean
isZero()
Check if this is a neutral element of addition, i.e.long
longValue()
Returns the whole number part of the fraction.Fraction
multiply(int value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.Fraction
multiply(Fraction value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.Fraction
negate()
Additive inverse.static Fraction
of(int num)
Create a fraction given the numerator.static Fraction
of(int num, int den)
Create a fraction given the numerator and denominator.Fraction
one()
Identity element.static Fraction
parse(String s)
Returns aFraction
instance representing the specified strings
.Fraction
pow(int exponent)
Returns aFraction
whose value isthisexponent
, returning the result in reduced form.Fraction
reciprocal()
Multiplicative inverse.int
signum()
Retrieves the sign of this fraction.Fraction
subtract(int value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.Fraction
subtract(Fraction value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.String
toString()
Returns theString
representing this fraction.Fraction
zero()
Identity element.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Method Detail
-
from
public static Fraction from(double value)
Create a fraction given the double value.- Parameters:
value
- Value to convert to a fraction.- Returns:
- a new instance.
- Throws:
IllegalArgumentException
- if the givenvalue
is NaN or infinite.ArithmeticException
- if the continued fraction failed to converge.
-
from
public static Fraction from(double value, double epsilon, int maxIterations)
Create a fraction given the double value and maximum error allowed.References:
- Continued Fraction equations (11) and (22)-(26)
- Parameters:
value
- Value to convert to a fraction.epsilon
- Maximum error allowed. The resulting fraction is withinepsilon
ofvalue
, in absolute terms.maxIterations
- Maximum number of convergents.- Returns:
- a new instance.
- Throws:
IllegalArgumentException
- if the givenvalue
is NaN or infinite;epsilon
is not positive; ormaxIterations < 1
.ArithmeticException
- if the continued fraction failed to converge.
-
from
public static Fraction from(double value, int maxDenominator)
Create a fraction given the double value and maximum denominator.References:
- Continued Fraction equations (11) and (22)-(26)
Note: The magnitude of the
maxDenominator
is used allowing use ofInteger.MIN_VALUE
for a supported maximum denominator of 231.- Parameters:
value
- Value to convert to a fraction.maxDenominator
- Maximum allowed value for denominator.- Returns:
- a new instance.
- Throws:
IllegalArgumentException
- if the givenvalue
is NaN or infinite ormaxDenominator
is zero.ArithmeticException
- if the continued fraction failed to converge.
-
of
public static Fraction of(int num)
Create a fraction given the numerator. The denominator is1
.- Parameters:
num
- Numerator.- Returns:
- a new instance.
-
of
public static Fraction of(int num, int den)
Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.- Parameters:
num
- Numerator.den
- Denominator.- Returns:
- a new instance.
- Throws:
ArithmeticException
- if the denominator iszero
.
-
parse
public static Fraction parse(String s)
Returns aFraction
instance representing the specified strings
.If
s
isnull
, then aNullPointerException
is thrown.The string must be in a format compatible with that produced by
Fraction.toString()
. The format expects an integer optionally followed by a'/'
character and and second integer. Leading and trailing spaces are allowed around each numeric part. Each numeric part is parsed usingInteger.parseInt(String)
. The parts are interpreted as the numerator and optional denominator of the fraction. If absent the denominator is assumed to be "1".Examples of valid strings and the equivalent
Fraction
are shown below:"0" = Fraction.of(0) "42" = Fraction.of(42) "0 / 1" = Fraction.of(0, 1) "1 / 3" = Fraction.of(1, 3) "-4 / 13" = Fraction.of(-4, 13)
Note: The fraction is returned in reduced form and the numerator and denominator may not match the values in the input string. For this reason the result of
Fraction.parse(s).toString().equals(s)
may not betrue
.- Parameters:
s
- String representation.- Returns:
- an instance.
- Throws:
NullPointerException
- if the string is null.NumberFormatException
- if the string does not contain a parsable fraction.- See Also:
Integer.parseInt(String)
,toString()
-
isZero
public boolean isZero()
Check if this is a neutral element of addition, i.e.this.add(a)
returnsa
or an element representing the same value asa
.The default implementation calls
equals(zero())
. Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations ofzero
and itsequals
method differentiates between them.- Specified by:
isZero
in interfaceAddition<Fraction>
- Returns:
true
ifthis
is a neutral element of addition.- See Also:
Addition.zero()
-
one
public Fraction one()
Description copied from interface:Multiplication
Identity element.- Specified by:
one
in interfaceMultiplication<Fraction>
- Returns:
- the field element such that for all
a
,one().multiply(a).equals(a)
istrue
.
-
isOne
public boolean isOne()
Check if this is a neutral element of multiplication, i.e.this.multiply(a)
returnsa
or an element representing the same value asa
.The default implementation calls
equals(one())
. Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations ofone
and itsequals
method differentiates between them.- Specified by:
isOne
in interfaceMultiplication<Fraction>
- Returns:
true
ifthis
is a neutral element of multiplication.- See Also:
Multiplication.one()
-
getNumerator
public int getNumerator()
Access the numerator as anint
.- Returns:
- the numerator as an
int
.
-
getDenominator
public int getDenominator()
Access the denominator as anint
.- Returns:
- the denominator as an
int
.
-
signum
public int signum()
Retrieves the sign of this fraction.- Returns:
- -1 if the value is strictly negative, 1 if it is strictly positive, 0 if it is 0.
-
reciprocal
public Fraction reciprocal()
Multiplicative inverse.Raises an exception if the fraction is equal to zero.
- Specified by:
reciprocal
in interfaceMultiplication<Fraction>
- Returns:
this-1
.- Throws:
ArithmeticException
- if the current numerator iszero
-
doubleValue
public double doubleValue()
Returns thedouble
value closest to this fraction. This calculates the fraction as numerator divided by denominator.- Specified by:
doubleValue
in classNumber
- Returns:
- the fraction as a
double
.
-
floatValue
public float floatValue()
Returns thefloat
value closest to this fraction. This calculates the fraction as numerator divided by denominator.- Specified by:
floatValue
in classNumber
- Returns:
- the fraction as a
float
.
-
intValue
public int intValue()
Returns the whole number part of the fraction.
-
longValue
public long longValue()
Returns the whole number part of the fraction.
-
add
public Fraction add(int value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Parameters:
value
- Value to add.- Returns:
this + value
.- Throws:
ArithmeticException
- if the resulting numerator cannot be represented in anint
.
-
add
public Fraction add(Fraction value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Specified by:
add
in interfaceAddition<Fraction>
- Parameters:
value
- Value to add.- Returns:
this + value
.- Throws:
ArithmeticException
- if the resulting numerator or denominator cannot be represented in anint
.
-
subtract
public Fraction subtract(int value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Parameters:
value
- Value to subtract.- Returns:
this - value
.- Throws:
ArithmeticException
- if the resulting numerator cannot be represented in anint
.
-
subtract
public Fraction subtract(Fraction value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Specified by:
subtract
in interfaceNativeOperators<Fraction>
- Parameters:
value
- Value to subtract.- Returns:
this - value
.- Throws:
ArithmeticException
- if the resulting numerator or denominator cannot be represented in anint
.
-
multiply
public Fraction multiply(int value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
multiply
in interfaceNativeOperators<Fraction>
- Parameters:
value
- Value to multiply by.- Returns:
this * value
.- Throws:
ArithmeticException
- if the resulting numerator cannot be represented in anint
.
-
multiply
public Fraction multiply(Fraction value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
multiply
in interfaceMultiplication<Fraction>
- Parameters:
value
- Value to multiply by.- Returns:
this * value
.- Throws:
ArithmeticException
- if the resulting numerator or denominator cannot be represented in anint
.
-
divide
public Fraction divide(int value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
ArithmeticException
- if the value to divide by is zero or if the resulting numerator or denominator cannot be represented by anint
.
-
divide
public Fraction divide(Fraction value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
divide
in interfaceNativeOperators<Fraction>
- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
ArithmeticException
- if the value to divide by is zero or if the resulting numerator or denominator cannot be represented by anint
.
-
pow
public Fraction pow(int exponent)
Returns aFraction
whose value isthisexponent
, returning the result in reduced form.- Specified by:
pow
in interfaceNativeOperators<Fraction>
- Parameters:
exponent
- exponent to which thisFraction
is to be raised.- Returns:
thisexponent
.- Throws:
ArithmeticException
- if the intermediate result would overflow.
-
toString
public String toString()
Returns theString
representing this fraction. Uses:"0"
ifnumerator
is zero."numerator"
ifdenominator
is one."numerator / denominator"
for all other cases.
-
compareTo
public int compareTo(Fraction other)
Compares this object with the specified object for order using the signed magnitude.- Specified by:
compareTo
in interfaceComparable<Fraction>
- Parameters:
other
-- Returns:
-
equals
public boolean equals(Object other)
Test for equality with another object. If the other object is aFraction
then a comparison is made of the sign and magnitude; otherwisefalse
is returned.
-
-