Class Rational
java.lang.Object
java.lang.Number
org.monte.media.math.Rational
- All Implemented Interfaces:
Serializable,Comparable<Rational>
Represents a Rational number
numerator/denominator.
A number is represented by two longs: the first represents the numerator of a fraction; the second, the denominator.
Invariants:
denominator >=0, the denominator is always a positive integer0/1is the unique* representation of 0.- {code 1/0},
-1/0 are the unique representations of positive and negative infinity.
- Author:
- Werner Randelshofer
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(long num, long den) ceil(long d) Returns the closest rational with the specified denominator which is greater or equal than this number.intreturn { -1, 0, +1 } if a < b, a = b, or a > b.doublebooleanfloatfloor(long d) Returns the closest rational with the specified denominator which is smaller or equal than this number.longfloorNumerator(long d) Returns the closest numerator for the specified denominator which is smaller or equal than this number.longlonginthashCode()intintValue()inverse()booleanbooleanisZero()longstatic Rationalstatic Rationalmultiply(long integer) negate()round(long d) toString()static RationalvalueOf(double d) static RationalvalueOf(long num, long den) static RationalParses a string.static RationalvalueOf(BigInteger num, BigInteger den) Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ONE
-
ZERO
-
serialVersionUID
public static final long serialVersionUID- See Also:
-
-
Constructor Details
-
Rational
public Rational(long numerator) -
Rational
public Rational(long numerator, long denominator) -
Rational
-
-
Method Details
-
getNumerator
public long getNumerator() -
getDenominator
public long getDenominator() -
add
-
add
-
subtract
-
negate
-
inverse
-
floor
Returns the closest rational with the specified denominator which is smaller or equal than this number. -
floorNumerator
public long floorNumerator(long d) Returns the closest numerator for the specified denominator which is smaller or equal than this number. -
ceil
Returns the closest rational with the specified denominator which is greater or equal than this number. -
multiply
-
multiply
-
divide
-
toString
-
toDescriptiveString
-
intValue
-
longValue
-
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
equals
-
compareTo
return { -1, 0, +1 } if a < b, a = b, or a > b.- Specified by:
compareToin interfaceComparable<Rational>
-
hashCode
-
max
-
min
-
isZero
public boolean isZero() -
isLessOrEqualZero
public boolean isLessOrEqualZero() -
valueOf
-
valueOf
-
valueOf
-
round
-
valueOf
Parses a string.A rational can be represented in the following ways:
- As a long number
- As a double number
- As an integer/integer rational number
- Throws:
NumberFormatException- if str can not be parsed.
-