public final class Fraction extends Number implements Comparable<Fraction>, NativeOperators<Fraction>, Serializable
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
Modifier and Type | Field and Description |
---|---|
static Fraction |
ONE
A fraction representing "1".
|
static Fraction |
ZERO
A fraction representing "0".
|
Modifier and Type | Method and Description |
---|---|
Fraction |
abs()
Returns the absolute value of this fraction.
|
Fraction |
add(Fraction value)
Adds the specified
value to this fraction, returning
the result in reduced form. |
Fraction |
add(int value)
Adds the specified
value 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(Fraction value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
Fraction |
divide(int value)
Divide this fraction by the passed
value , returning
the result in reduced form. |
double |
doubleValue()
Returns the
double value closest to this fraction. |
boolean |
equals(Object other)
Test for equality with another object.
|
float |
floatValue()
Returns the
float 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 an
int . |
int |
getNumerator()
Access the numerator as an
int . |
int |
hashCode() |
int |
intValue()
Returns the whole number part of the fraction.
|
long |
longValue()
Returns the whole number part of the fraction.
|
Fraction |
multiply(Fraction value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
Fraction |
multiply(int value)
Multiply this fraction by the passed
value , returning
the result in reduced form. |
Fraction |
negate() |
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() |
static Fraction |
parse(String s)
Returns a
Fraction instance representing the specified string s . |
Fraction |
pow(int exponent)
Returns a
Fraction whose value is
thisexponent , returning the result in reduced form. |
Fraction |
reciprocal() |
int |
signum()
Retrieves the sign of this fraction.
|
Fraction |
subtract(Fraction value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
Fraction |
subtract(int value)
Subtracts the specified
value from this fraction, returning
the result in reduced form. |
String |
toString()
Returns the
String representing this fraction. |
Fraction |
zero() |
byteValue, shortValue
public static Fraction from(double value)
value
- Value to convert to a fraction.IllegalArgumentException
- if the given value
is NaN or infinite.ArithmeticException
- if the continued fraction failed to converge.public static Fraction from(double value, double epsilon, int maxIterations)
References:
value
- Value to convert to a fraction.epsilon
- Maximum error allowed. The resulting fraction is within
epsilon
of value
, in absolute terms.maxIterations
- Maximum number of convergents.IllegalArgumentException
- if the given value
is NaN or infinite;
epsilon
is not positive; or maxIterations < 1
.ArithmeticException
- if the continued fraction failed to converge.public static Fraction from(double value, int maxDenominator)
References:
Note: The magnitude of the maxDenominator
is used allowing use of
Integer.MIN_VALUE
for a supported maximum denominator of 231.
value
- Value to convert to a fraction.maxDenominator
- Maximum allowed value for denominator.IllegalArgumentException
- if the given value
is NaN or infinite
or maxDenominator
is zero.ArithmeticException
- if the continued fraction failed to converge.public static Fraction of(int num)
1
.num
- Numerator.public static Fraction of(int num, int den)
num
- Numerator.den
- Denominator.ArithmeticException
- if the denominator is zero
.public static Fraction parse(String s)
Fraction
instance representing the specified string s
.
If s
is null
, then a NullPointerException
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 using Integer.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 be true
.
s
- String representation.NullPointerException
- if the string is null.NumberFormatException
- if the string does not contain a parsable fraction.Integer.parseInt(String)
,
toString()
public Fraction one()
one
in interface Multiplication<Fraction>
public int getNumerator()
int
.int
.public int getDenominator()
int
.int
.public int signum()
public Fraction reciprocal()
Raises an exception if the fraction is equal to zero.
reciprocal
in interface Multiplication<Fraction>
ArithmeticException
- if the current numerator is zero
public double doubleValue()
double
value closest to this fraction.
This calculates the fraction as numerator divided by denominator.doubleValue
in class Number
double
.public float floatValue()
float
value closest to this fraction.
This calculates the fraction as numerator divided by denominator.floatValue
in class Number
float
.public int intValue()
public long longValue()
public Fraction add(int value)
value
to this fraction, returning
the result in reduced form.value
- Value to add.this + value
.ArithmeticException
- if the resulting numerator
cannot be represented in an int
.public Fraction add(Fraction value)
value
to this fraction, returning
the result in reduced form.add
in interface Addition<Fraction>
value
- Value to add.this + value
.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction subtract(int value)
value
from this fraction, returning
the result in reduced form.value
- Value to subtract.this - value
.ArithmeticException
- if the resulting numerator
cannot be represented in an int
.public Fraction subtract(Fraction value)
value
from this fraction, returning
the result in reduced form.subtract
in interface NativeOperators<Fraction>
value
- Value to subtract.this - value
.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction multiply(int value)
value
, returning
the result in reduced form.multiply
in interface NativeOperators<Fraction>
value
- Value to multiply by.this * value
.ArithmeticException
- if the resulting numerator
cannot be represented in an int
.public Fraction multiply(Fraction value)
value
, returning
the result in reduced form.multiply
in interface Multiplication<Fraction>
value
- Value to multiply by.this * value
.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction divide(int value)
value
, returning
the result in reduced form.value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zero
or if the resulting numerator or denominator cannot be represented
by an int
.public Fraction divide(Fraction value)
value
, returning
the result in reduced form.divide
in interface NativeOperators<Fraction>
value
- Value to divide bythis / value
.ArithmeticException
- if the value to divide by is zero
or if the resulting numerator or denominator cannot be represented
by an int
.public Fraction pow(int exponent)
Fraction
whose value is
thisexponent
, returning the result in reduced form.pow
in interface NativeOperators<Fraction>
exponent
- exponent to which this Fraction
is to be raised.thisexponent
.ArithmeticException
- if the intermediate result would overflow.public String toString()
String
representing this fraction.
Uses:
"0"
if numerator
is zero.
"numerator"
if denominator
is one.
"numerator / denominator"
for all other cases.
public int compareTo(Fraction other)
compareTo
in interface Comparable<Fraction>
other
- public boolean equals(Object other)
Fraction
then a
comparison is made of the sign and magnitude; otherwise false
is returned.Copyright © 2017–2022 The Apache Software Foundation. All rights reserved.