Class Fraction
- All Implemented Interfaces:
Serializable,Comparable<Fraction>,Addition<Fraction>,Multiplication<Fraction>,NativeOperators<Fraction>
The number is expressed as the quotient p/q of two 32-bit integers,
a numerator p and a non-zero denominator q.
This class is immutable. Rational number
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns the absolute value of this fraction.add(int value) Adds the specifiedvalueto this fraction, returning the result in reduced form.Adds the specifiedvalueto this fraction, returning the result in reduced form.intCompares this object with the specified object for order using the signed magnitude.divide(int value) Divide this fraction by the passedvalue, returning the result in reduced form.Divide this fraction by the passedvalue, returning the result in reduced form.doubleReturns thedoublevalue closest to this fraction.booleanTest for equality with another object.floatReturns thefloatvalue closest to this fraction.static Fractionfrom(double value) Create a fraction given the double value.static Fractionfrom(double value, double epsilon, int maxIterations) Create a fraction given the double value and maximum error allowed.static Fractionfrom(double value, int maxDenominator) Create a fraction given the double value and maximum denominator.intAccess the denominator as anint.intAccess the numerator as anint.inthashCode()intintValue()Returns the whole number part of the fraction.booleanisOne()Check if this is a neutral element of multiplication, i.e.booleanisZero()Check if this is a neutral element of addition, i.e.longReturns the whole number part of the fraction.multiply(int value) Multiply this fraction by the passedvalue, returning the result in reduced form.Multiply this fraction by the passedvalue, returning the result in reduced form.negate()Additive inverse.static Fractionof(int num) Create a fraction given the numerator.static Fractionof(int num, int den) Create a fraction given the numerator and denominator.one()Identity element.static FractionReturns aFractioninstance representing the specified strings.pow(int exponent) Returns aFractionwhose value isthisexponent, returning the result in reduced form.Multiplicative inverse.intsignum()Retrieves the sign of this fraction.subtract(int value) Subtracts the specifiedvaluefrom this fraction, returning the result in reduced form.Subtracts the specifiedvaluefrom this fraction, returning the result in reduced form.toString()Returns theStringrepresenting this fraction.zero()Identity element.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
A fraction representing "0". -
ONE
A fraction representing "1".
-
-
Method Details
-
from
Create a fraction given the double value.- Parameters:
value- Value to convert to a fraction.- Returns:
- a new instance.
- Throws:
IllegalArgumentException- if the givenvalueis NaN or infinite.ArithmeticException- if the continued fraction failed to converge.
-
from
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 withinepsilonofvalue, in absolute terms.maxIterations- Maximum number of convergents.- Returns:
- a new instance.
- Throws:
IllegalArgumentException- if the givenvalueis NaN or infinite;epsilonis not positive; ormaxIterations < 1.ArithmeticException- if the continued fraction failed to converge.
-
from
Create a fraction given the double value and maximum denominator.References:
- Continued Fraction equations (11) and (22)-(26)
Note: The magnitude of the
maxDenominatoris used allowing use ofInteger.MIN_VALUEfor 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 givenvalueis NaN or infinite ormaxDenominatoris zero.ArithmeticException- if the continued fraction failed to converge.
-
of
Create a fraction given the numerator. The denominator is1.- Parameters:
num- Numerator.- Returns:
- a new instance.
-
of
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
Returns aFractioninstance representing the specified strings.If
sisnull, then aNullPointerExceptionis 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
Fractionare 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:
-
zero
Description copied from interface:AdditionIdentity element. -
isZero
Check if this is a neutral element of addition, i.e.this.add(a)returnsaor 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 ofzeroand itsequalsmethod differentiates between them. -
one
Description copied from interface:MultiplicationIdentity element.- Specified by:
onein interfaceMultiplication<Fraction>- Returns:
- the field element such that for all
a,one().multiply(a).equals(a)istrue.
-
isOne
Check if this is a neutral element of multiplication, i.e.this.multiply(a)returnsaor 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 ofoneand itsequalsmethod differentiates between them.- Specified by:
isOnein interfaceMultiplication<Fraction>- Returns:
trueifthisis a neutral element of multiplication.- See Also:
-
getNumerator
Access the numerator as anint.- Returns:
- the numerator as an
int.
-
getDenominator
Access the denominator as anint.- Returns:
- the denominator as an
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.
-
abs
Returns the absolute value of this fraction.- Returns:
- the absolute value.
-
negate
Description copied from interface:AdditionAdditive inverse. -
reciprocal
Multiplicative inverse.Raises an exception if the fraction is equal to zero.
- Specified by:
reciprocalin interfaceMultiplication<Fraction>- Returns:
this-1.- Throws:
ArithmeticException- if the current numerator iszero
-
doubleValue
Returns thedoublevalue closest to this fraction. This calculates the fraction as numerator divided by denominator.- Specified by:
doubleValuein classNumber- Returns:
- the fraction as a
double.
-
floatValue
Returns thefloatvalue closest to this fraction. This calculates the fraction as numerator divided by denominator.- Specified by:
floatValuein classNumber- Returns:
- the fraction as a
float.
-
intValue
Returns the whole number part of the fraction. -
longValue
Returns the whole number part of the fraction. -
add
Adds the specifiedvalueto 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
Adds the specifiedvalueto this fraction, returning the result in reduced form.- Specified by:
addin interfaceAddition<Fraction>- Parameters:
value- Value to add.- Returns:
this + value.- Throws:
ArithmeticException- if the resulting numerator or denominator cannot be represented in anint.
-
subtract
Subtracts the specifiedvaluefrom 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
Subtracts the specifiedvaluefrom this fraction, returning the result in reduced form.- Specified by:
subtractin interfaceNativeOperators<Fraction>- Parameters:
value- Value to subtract.- Returns:
this - value.- Throws:
ArithmeticException- if the resulting numerator or denominator cannot be represented in anint.
-
multiply
Multiply this fraction by the passedvalue, returning the result in reduced form.- Specified by:
multiplyin interfaceNativeOperators<Fraction>- Parameters:
value- Value to multiply by.- Returns:
this * value.- Throws:
ArithmeticException- if the resulting numerator cannot be represented in anint.
-
multiply
Multiply this fraction by the passedvalue, returning the result in reduced form.- Specified by:
multiplyin 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
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
Divide this fraction by the passedvalue, returning the result in reduced form.- Specified by:
dividein 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
Returns aFractionwhose value isthisexponent, returning the result in reduced form.- Specified by:
powin interfaceNativeOperators<Fraction>- Parameters:
exponent- exponent to which thisFractionis to be raised.- Returns:
thisexponent.- Throws:
ArithmeticException- if the intermediate result would overflow.
-
toString
Returns theStringrepresenting this fraction. Uses:"0"ifnumeratoris zero."numerator"ifdenominatoris one."numerator / denominator"for all other cases.
-
compareTo
Compares this object with the specified object for order using the signed magnitude.- Specified by:
compareToin interfaceComparable<Fraction>- Parameters:
other-- Returns:
-
equals
Test for equality with another object. If the other object is aFractionthen a comparison is made of the sign and magnitude; otherwisefalseis returned. -
hashCode
-