Package com.github.kokorin.jaffree
Class Rational
java.lang.Object
java.lang.Number
com.github.kokorin.jaffree.Rational
- All Implemented Interfaces:
Serializable,Comparable<Rational>
Represents rational numbers.
Double and Float can't represent every rational number without precision loss,
which is crucial for correct media representation.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns aRationalwhose value is(this + value).intReturns aRationalwhose value is(this / value).doublebooleanfloatlongReturns thisRationaldenominator.longReturns thisRationalnumerator.booleangreaterThan(Number that) Returns true if(this > value).booleangreaterThanOrEqual(Number that) Returns true if(this >= value).inthashCode()intintValue()inverse()Returns aRationalwhose value is(1 / this).booleanReturns true if(this < value).booleanlessThanOrEqual(Number that) Returns true if(this <= value).longReturns aRationalwhose value is(this * value).negate()Returns aRationalwhose value is(-this).simplify()Simplifies thisRationalby dividing numerator and denominator by greatest common divisor.Returns aRationalwhose value is(this - value).toString()static RationalvalueOf(double value) Returns aRationalwhose value is equal to that of the specifieddouble.static RationalvalueOf(long value) Returns aRationalwhose value is equal to that of the specifiedlong.static RationalReturns aRationalwhose value is equal to that of the specifiedNumber.static RationalParsesRational.static RationalParsesRational.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
-
ONE
-
-
Constructor Details
-
Rational
public Rational(long numerator, long denominator) CreateRational.- Parameters:
numerator- numeratordenominator- denominator
-
-
Method Details
-
getNumerator
public long getNumerator()Returns thisRationalnumerator.- Returns:
- numerator
-
getDenominator
public long getDenominator()Returns thisRationaldenominator.- Returns:
- denominator
-
compareTo
- Specified by:
compareToin interfaceComparable<Rational>
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
negate
Returns aRationalwhose value is(-this).- Returns:
-this
-
add
Returns aRationalwhose value is(this + value).- Parameters:
value- value to be added to thisRational.- Returns:
this + val
-
subtract
Returns aRationalwhose value is(this - value).- Parameters:
value- value to be subtracted from thisRational.- Returns:
this - val
-
multiply
Returns aRationalwhose value is(this * value).- Parameters:
value- value to be multiplied by thisRational.- Returns:
this * value
-
divide
Returns aRationalwhose value is(this / value).- Parameters:
value- divisor- Returns:
this / value
-
inverse
Returns aRationalwhose value is(1 / this).- Returns:
1 / this
-
lessThan
Returns true if(this < value).- Parameters:
that- value to be compared with thisRational.- Returns:
this < value
-
lessThanOrEqual
Returns true if(this <= value).- Parameters:
that- value to be compared with thisRational.- Returns:
this <= value
-
greaterThan
Returns true if(this > value).- Parameters:
that- value to be compared with thisRational.- Returns:
this > value
-
greaterThanOrEqual
Returns true if(this >= value).- Parameters:
that- value to be compared with thisRational.- Returns:
this >= value
-
simplify
Simplifies thisRationalby dividing numerator and denominator by greatest common divisor.E.g.
2/6is simplified to1/3- Returns:
- simplified
Rational
-
toString
-
toString
- Parameters:
delimiter- delimiter- Returns:
- this converted to
String
-
equals
-
hashCode
public int hashCode() -
valueOf
Returns aRationalwhose value is equal to that of the specifiedlong. -
valueOf
Returns aRationalwhose value is equal to that of the specifieddouble. -
valueOf
Returns aRationalwhose value is equal to that of the specifiedNumber. -
valueOf
ParsesRational.Numerator and denominator are expected to be separated by / (slash) symbol.
- Parameters:
value- value to parse- Returns:
- Rational value
- Throws:
NumberFormatException- if wrong format
-
valueOf
ParsesRational.Numerator and denominator are expected to be separated by delimiter.
- Parameters:
value- value to parsedelimiter- delimiter which separates numerator and denominator- Returns:
- Rational value
- Throws:
NumberFormatException- if wrong format
-