Class RationalNumber
- java.lang.Object
-
- java.lang.Number
-
- org.apache.commons.imaging.common.RationalNumber
-
- All Implemented Interfaces:
java.io.Serializable
public class RationalNumber extends java.lang.NumberRational number, as used by the TIFF image format.The TIFF format specifies two data types for rational numbers based on a pair of 32-bit integers. Rational is based on unsigned 32-bit integers and SRational is based on signed 32-bit integers. This treatment is problematic in Java because Java does not support unsigned types. To address this challenge, this class stores the numerator and divisor in long (64-bit) integers, applying masks as necessary for the unsigned type.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classRationalNumber.Option
-
Field Summary
Fields Modifier and Type Field Description longdivisorlongnumeratorprivate static longserialVersionUIDstatic intSHALLOW_SIZEprivate static doubleTOLERANCEbooleanunsignedType
-
Constructor Summary
Constructors Modifier Constructor Description RationalNumber(int numerator, int divisor)Constructs an instance based on signed integersRationalNumber(int numerator, int divisor, boolean unsignedType)Constructs an instance supports either signed or unsigned integers.privateRationalNumber(long numerator, long divisor, boolean unsignedType)A private constructor for methods such as negate() that create instances of this class using the content of the current instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubledoubleValue()(package private) static RationalNumberfactoryMethod(long n, long d)floatfloatValue()private static longgcd(long a, long b)Gets the greatest common divisorintintValue()longlongValue()RationalNumbernegate()Negates the value of the RationalNumber.java.lang.StringtoDisplayString()java.lang.StringtoString()static RationalNumbervalueOf(double value)Calculate rational number using successive approximations.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
TOLERANCE
private static final double TOLERANCE
- See Also:
- Constant Field Values
-
SHALLOW_SIZE
public static final int SHALLOW_SIZE
- See Also:
- Constant Field Values
-
numerator
public final long numerator
-
divisor
public final long divisor
-
unsignedType
public final boolean unsignedType
-
-
Constructor Detail
-
RationalNumber
public RationalNumber(int numerator, int divisor)Constructs an instance based on signed integers- Parameters:
numerator- a 32-bit signed integerdivisor- a non-zero 32-bit signed integer
-
RationalNumber
public RationalNumber(int numerator, int divisor, boolean unsignedType)Constructs an instance supports either signed or unsigned integers.- Parameters:
numerator- a numerator in the indicated form (signed or unsigned)divisor- a non-zero divisor in the indicated form (signed or unsigned)unsignedType- indicates whether the input values are to be treated as unsigned.
-
RationalNumber
private RationalNumber(long numerator, long divisor, boolean unsignedType)A private constructor for methods such as negate() that create instances of this class using the content of the current instance.- Parameters:
numerator- a valid numeratordivisor- a valid denominatorunsignedType- indicates how numerator and divisor values are to be interpreted.
-
-
Method Detail
-
factoryMethod
static RationalNumber factoryMethod(long n, long d)
-
gcd
private static long gcd(long a, long b)Gets the greatest common divisor
-
valueOf
public static RationalNumber valueOf(double value)
Calculate rational number using successive approximations.- Parameters:
value- rational number double value- Returns:
- the RationalNumber representation of the double value
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classjava.lang.Number
-
floatValue
public float floatValue()
- Specified by:
floatValuein classjava.lang.Number
-
intValue
public int intValue()
- Specified by:
intValuein classjava.lang.Number
-
longValue
public long longValue()
- Specified by:
longValuein classjava.lang.Number
-
negate
public RationalNumber negate()
Negates the value of the RationalNumber. If the numerator of this instance has its high-order bit set, then its value is too large to be treated as a Java 32-bit signed integer. In such a case, the only way that a RationalNumber instance can be negated is to divide both terms by a common divisor, if a non-zero common divisor exists. However, if no such divisor exists, there is no numerically correct way to perform the negation. When a negation cannot be performed correctly, this method throws an unchecked exception.- Returns:
- a valid instance with a negated value.
-
toDisplayString
public java.lang.String toDisplayString()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-