Class Rational
java.lang.Object
java.lang.Number
com.twelvemonkeys.imageio.metadata.tiff.Rational
- All Implemented Interfaces:
Serializable, Comparable<Rational>
Represents a rational number with a
long numerator and long denominator.
Rational numbers are stored in reduced form with the sign stored with the numerator.
Rationals are immutable.
Adapted from sample code featured in "Intro to Programming in Java: An Interdisciplinary Approach" (Addison Wesley) by Robert Sedgewick and Kevin Wayne. Permission granted to redistribute under BSD license.
- Version:
- $Id: Rational.java,v 1.0 Nov 18, 2009 1:12:00 AM haraldk Exp$
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintComparable implementationlongdoublebooleanfloatprivate static longgcd(long m, long n) inthashCode()Object overridesintintValue()Number implementationprivate static longlcm(long m, long n) longnegate()longOperations (adapted from http://www.cs.princeton.edu/introcs/92symbolic/Rational.java.html)toString()Methods inherited from class Number
byteValue, shortValue
-
Field Details
-
ZERO
-
NaN
-
numerator
private final long numerator -
denominator
private final long denominator
-
-
Constructor Details
-
Rational
private Rational() -
Rational
public Rational(long pNumber) -
Rational
public Rational(long pNumerator, long pDenominator)
-
-
Method Details
-
gcd
private static long gcd(long m, long n) -
lcm
private static long lcm(long m, long n) -
numerator
public long numerator() -
denominator
public long denominator() -
intValue
-
longValue
-
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
compareTo
Comparable implementation- Specified by:
compareToin interfaceComparable<Rational>
-
hashCode
-
equals
-
toString
-
times
-
plus
-
negate
-
minus
-
reciprocal
-
divides
-