Package org.apache.commons.imaging.common

# Class RationalNumber

java.lang.Object
java.lang.Number
org.apache.commons.imaging.common.RationalNumber

All Implemented Interfaces:
  `Serializable`

public class RationalNumber extends Number
Rational 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

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `final long`
  `divisor`
  `final long`
  `numerator`
  `static final int`
  `SHALLOW_SIZE`
  `final boolean`
  `unsignedType`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `RationalNumber(int numerator, int divisor)`
  Constructs an instance based on signed integers
  `RationalNumber(int numerator, int divisor, boolean unsignedType)`
  Constructs an instance supports either signed or unsigned integers.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `double`
  `doubleValue()`
  `float`
  `floatValue()`
  `int`
  `intValue()`
  `long`
  `longValue()`
  `RationalNumber`
  `negate()`
  Negates the value of the RationalNumber.
  `String`
  `toDisplayString()`
  `String`
  `toString()`
  `static RationalNumber`
  `valueOf(double value)`
  Calculate rational number using successive approximations.

  ### Methods inherited from class java.lang.Number

  `byteValue, shortValue`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### 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 Details

  - ### RationalNumber

    public RationalNumber(int numerator, int divisor)
    Constructs an instance based on signed integers
    Parameters:
      `numerator` - a 32-bit signed integer
      `divisor` - 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.

- ## Method Details

  - ### 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:
      `doubleValue` in class `Number`

  - ### floatValue

    public float floatValue()
    Specified by:
      `floatValue` in class `Number`

  - ### intValue

    public int intValue()
    Specified by:
      `intValue` in class `Number`

  - ### longValue

    public long longValue()
    Specified by:
      `longValue` in class `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 String toDisplayString()

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`