public final class Fraction extends Number implements Comparable<Fraction>, Serializable
Fraction
objects are represented by a numerator and
a denominator stored at 32 bits integers. Fractions can be simplified.
All Fraction
instances are immutable and thus inherently thread-safe.Defined in the sis-utility
module
Modifier and Type | Field and Description |
---|---|
int |
denominator
The b term in the a/b fraction.
|
int |
numerator
The a term in the a/b fraction.
|
Constructor and Description |
---|
Fraction(int numerator,
int denominator)
Creates a new fraction.
|
Modifier and Type | Method and Description |
---|---|
Fraction |
add(Fraction other)
Returns the simplified result of adding the given fraction to this fraction.
|
byte |
byteValue()
Returns this fraction rounded toward zero, if the result can be represented as a signed byte.
|
int |
ceil()
Returns this fraction rounded toward positive infinity.
|
int |
compareTo(Fraction other)
Compares this fraction with the given one for order.
|
Fraction |
divide(Fraction other)
Returns the simplified result of dividing this fraction by the given fraction.
|
double |
doubleValue()
Returns the fraction as a double-precision floating point number.
|
boolean |
equals(Object other)
Compares this fraction with the given object for equality.
|
float |
floatValue()
Returns the fraction as a single-precision floating point number.
|
int |
floor()
Returns this fraction rounded toward negative infinity.
|
int |
hashCode()
Returns a hash code value for this fraction.
|
int |
intValue()
Returns this fraction rounded toward zero.
|
long |
longValue()
Returns this fraction rounded toward zero.
|
Fraction |
multiply(Fraction other)
Returns the simplified result of multiplying the given fraction with this fraction.
|
Fraction |
negate()
Returns the negative value of this fraction.
|
int |
round()
Returns this fraction rounded toward nearest integer.
|
short |
shortValue()
Returns this fraction rounded toward zero, if the result can be represented as a short integer.
|
Fraction |
simplify()
Returns a fraction equivalent to
this but represented by the smallest possible numerator
and denominator values. |
Fraction |
subtract(Fraction other)
Returns the simplified result of subtracting the given fraction from this fraction.
|
String |
toString()
Returns a string representation of this fraction.
|
Fraction |
unique()
Returns a unique fraction instance equals to
this . |
public final int numerator
doubleValue()
public final int denominator
doubleValue()
public Fraction(int numerator, int denominator)
simplify()
.numerator
- the a term in the a/b fraction.denominator
- the b term in the a/b fraction.public Fraction unique()
this
.
If this method has been invoked previously on another Fraction
with the same value than this
,
then that previous instance is returned (provided that it has not yet been garbage collected). Otherwise this
method adds this fraction to the pool of fractions that may be returned in next unique()
invocations,
then returns this
.
This method is useful for saving memory when a potentially large amount of Fraction
instances will
be kept for a long time and many instances are likely to have the same values.
It is usually not worth to invoke this method for short-lived instances.
this
.public Fraction simplify()
this
but represented by the smallest possible numerator
and denominator values. If this fraction can not be simplified, then this method returns this
.public Fraction negate()
-this
.ArithmeticException
- if the result overflows.public Fraction add(Fraction other)
other
- the fraction to add to this fraction.this
+ other
.ArithmeticException
- if the result overflows.public Fraction subtract(Fraction other)
other
- the fraction to subtract from this fraction.this
- other
.ArithmeticException
- if the result overflows.public Fraction multiply(Fraction other)
other
- the fraction to multiply with this fraction.this
× other
.ArithmeticException
- if the result overflows.public Fraction divide(Fraction other)
other
- the fraction by which to divide this fraction.this
∕ other
.ArithmeticException
- if the result overflows.public int round()
numerator
/ denominator
rounded toward nearest integer.public int floor()
Tip: if the numerator and the denominator are both positive or both negative,
then the result is positive and identical to numerator / denominator
.
numerator
/ denominator
rounded toward negative infinity.public int ceil()
numerator
/ denominator
rounded toward positive infinity.public double doubleValue()
Double.NaN
.doubleValue
in class Number
public float floatValue()
Float.NaN
.floatValue
in class Number
public long longValue()
longValue
in class Number
public int intValue()
public short shortValue()
shortValue
in class Number
ArithmeticException
- if the result can not be represented as a short integer.public byte byteValue()
byteValue
in class Number
ArithmeticException
- if the result can not be represented as a signed byte.public int compareTo(Fraction other)
compareTo
in interface Comparable<Fraction>
other
- the fraction to compare to this fraction for ordering.public boolean equals(Object other)
true
only if
the two objects are fractions with same numerator and denominator values.
Fractions with different values are not considered equal even if the two fraction are equivalent.public int hashCode()
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.