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 longprotected final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleasDouble()Return the value of the Fraction as a doubleclone()intcompareTo(long n) return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.intreturn a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.final longReturn the denominatordividedBy(long n) return a Fraction representing this Fraction divided by nreturn a Fraction representing this Fraction divided by bbooleanequals(long n) booleanstatic longgcd(long a, long b) Compute the nonnegative greatest common divisor of a and b.inthashCode()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 longReturn 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
-
plus
return a Fraction representing this Fraction plus n -
minus
-
minus
return a Fraction representing this Fraction minus n -
times
-
times
return a Fraction representing this Fraction times n -
dividedBy
-
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:
compareToin 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
-