Class Fraction
- java.lang.Object
-
- EDU.oswego.cs.dl.util.concurrent.misc.Fraction
-
- All Implemented Interfaces:
Serializable,Cloneable,Comparable
public class Fraction extends Object implements Cloneable, Comparable, Serializable
An immutable class representing fractions as pairs of longs. Fractions are always maintained in reduced form.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected longdenominator_protected longnumerator_
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleasDouble()Return the value of the Fraction as a doubleObjectclone()intcompareTo(long n)return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.intcompareTo(Object other)return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.longdenominator()Return the denominatorFractiondividedBy(long n)return a Fraction representing this Fraction divided by nFractiondividedBy(Fraction b)return a Fraction representing this Fraction divided by bbooleanequals(long n)booleanequals(Object other)static longgcd(long a, long b)Compute the nonnegative greatest common divisor of a and b.inthashCode()Fractioninverse()return a Fraction representing 1 / this FractionFractionminus(long n)return a Fraction representing this Fraction minus nFractionminus(Fraction b)return a Fraction representing this Fraction minus bFractionnegative()return a Fraction representing the negated value of this Fractionlongnumerator()Return the numeratorFractionplus(long n)return a Fraction representing this Fraction plus nFractionplus(Fraction b)return a Fraction representing this Fraction plus bFractiontimes(long n)return a Fraction representing this Fraction times nFractiontimes(Fraction b)return a Fraction representing this Fraction times bStringtoString()
-
-
-
Constructor Detail
-
Fraction
public Fraction(long num, long den)Create a Fraction equal in value to num / den
-
Fraction
public Fraction(Fraction f)
Create a fraction with the same value as Fraction f
-
-
Method Detail
-
numerator
public final long numerator()
Return the numerator
-
denominator
public final long denominator()
Return the denominator
-
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
public Fraction negative()
return a Fraction representing the negated value of this Fraction
-
inverse
public Fraction inverse()
return a Fraction representing 1 / this Fraction
-
plus
public Fraction plus(long n)
return a Fraction representing this Fraction plus n
-
minus
public Fraction minus(long n)
return a Fraction representing this Fraction minus n
-
times
public Fraction times(long n)
return a Fraction representing this Fraction times n
-
dividedBy
public Fraction dividedBy(Fraction b)
return a Fraction representing this Fraction divided by b
-
dividedBy
public Fraction dividedBy(long n)
return a Fraction representing this Fraction divided by n
-
compareTo
public int compareTo(Object other)
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
public boolean equals(long n)
-
-