Class Fraction
java.lang.Object
EDU.oswego.cs.dl.util.concurrent.misc.Fraction
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable
An immutable class representing fractions as pairs of longs.
Fractions are always maintained in reduced form.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final long
protected final long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondouble
asDouble()
Return the value of the Fraction as a doubleclone()
int
compareTo
(long n) return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.int
return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.final long
Return the denominatordividedBy
(long n) return a Fraction representing this Fraction divided by nreturn a Fraction representing this Fraction divided by bboolean
equals
(long n) boolean
static long
gcd
(long a, long b) Compute the nonnegative greatest common divisor of a and b.int
hashCode()
inverse()
return a Fraction representing 1 / this Fractionminus
(long n) return a Fraction representing this Fraction minus nreturn a Fraction representing this Fraction minus bnegative()
return a Fraction representing the negated value of this Fractionfinal long
Return the numeratorplus
(long n) return a Fraction representing this Fraction plus nreturn a Fraction representing this Fraction plus btimes
(long n) return a Fraction representing this Fraction times nreturn a Fraction representing this Fraction times btoString()
-
Field Details
-
numerator_
protected final long numerator_ -
denominator_
protected final long denominator_
-
-
Constructor Details
-
Fraction
public Fraction(long num, long den) Create a Fraction equal in value to num / den -
Fraction
Create a fraction with the same value as Fraction f
-
-
Method Details
-
numerator
public final long numerator()Return the numerator -
denominator
public final long denominator()Return the denominator -
toString
-
clone
-
asDouble
public double asDouble()Return the value of the Fraction as a double -
gcd
public static long gcd(long a, long b) Compute the nonnegative greatest common divisor of a and b. (This is needed for normalizing Fractions, but can be useful on its own.) -
negative
return a Fraction representing the negated value of this Fraction -
inverse
return a Fraction representing 1 / this Fraction -
plus
return a Fraction representing this Fraction plus b -
plus
return a Fraction representing this Fraction plus n -
minus
return a Fraction representing this Fraction minus b -
minus
return a Fraction representing this Fraction minus n -
times
return a Fraction representing this Fraction times b -
times
return a Fraction representing this Fraction times n -
dividedBy
return a Fraction representing this Fraction divided by b -
dividedBy
return a Fraction representing this Fraction divided by n -
compareTo
return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.- Specified by:
compareTo
in interfaceComparable
-
compareTo
public int compareTo(long n) return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n. -
equals
-
equals
public boolean equals(long n) -
hashCode
public int hashCode()
-