Record Class QuadrupleImmutable

java.lang.Object
java.lang.Record
gaiasky.util.math.QuadrupleImmutable
Record Components:
negative - The sign of the value (true signifies negative values).
exponent - The binary exponent (unbiased).
mantHi - The most significant 64 bits of fractional part of the mantissa.
mantLo - The least significant 64 bits of fractional part of the mantissa.
All Implemented Interfaces:
Comparable<QuadrupleImmutable>

public record QuadrupleImmutable(boolean negative, int exponent, long mantHi, long mantLo) extends Record implements Comparable<QuadrupleImmutable>
Immutable, thread-safe version of Quadruple implemented as a Java record.

A floating-point number with a 128-bit fractional part of the mantissa and 32-bit exponent. Normal values range from approximately 2.271e-646456993 to 1.761e+646456993 with precision not worse than 1,469368e-39 (2^-129, half the less significant bit).

Like standard double, it can store and process subnormal values, with lower precision. The subnormal values range from 6.673e-646457032 to 2.271e-646456993, they use less than 129 bits of the mantissa and their precision depends on the number of used bits, the less the value the worst the precision.

Implements conversions from/to other numeric types, conversions from/to strings, formatting, arithmetic operations and square root.

The biased exponent values stored in the exponent field are as following:

biased value const name means unbiased exponent (power of 2)
0x0000_0000 EXPONENT_OF_SUBNORMAL subnormal values 0x8000_0001 = -2147483647 = Integer.MIN_VALUE + 1
0x0000_0001 EXPONENT_OF_MIN_NORMAL MIN_NORMAL 0x8000_0002 = -2147483646 = Integer.MIN_VALUE + 2
0x7FFF_FFFE   -1 0xFFFF_FFFF
0x7FFF_FFFF EXPONENT_OF_ONE 0 0x0000_0000
0x8000_0000   1 0x0000_0001
0xFFFF_FFFE EXPONENT_OF_MAX_VALUE MAX_VALUE 0x7fff_ffff = 2147483647 = Integer.MAX_VALUE
0xFFFF_FFFF EXPONENT_OF_INFINITY Infinity 0x8000_0000 = 2147483648 = Integer.MIN_VALUE
Quadruple constants and information.

The boundaries of the range are:
MAX_VALUE:  2^2147483647 * (2 - 2^-128) =
            = 1.76161305168396335320749314979184028566452310e+646456993
MIN_NORMAL: 2^-2147483646 =
            = 2.27064621040149253752656726517958758124747730e-646456993
MIN_VALUE:  2^-2147483774 =
            = 6.67282948260747430814835377499134611597699952e-646457032

Original code by M. Vokhmentsev, see this repository.

  • Field Details

    • EXPONENT_OF_ONE

      public static final int EXPONENT_OF_ONE
      The value of the exponent (biased) corresponding to 1.0 == 2^0; equals to 2_147_483_647 (0x7FFF_FFFF). The same as EXPONENT_BIAS
      See Also:
    • EXPONENT_BIAS

      public static final int EXPONENT_BIAS
      The value of the exponent (biased) corresponding to 1.0 == 2^0; equals to 2_147_483_647 (0x7FFF_FFFF) The same as EXPONENT_OF_ONE
      See Also:
    • EXPONENT_OF_MAX_VALUE

      public static final long EXPONENT_OF_MAX_VALUE
      The value of the exponent (biased), corresponding to MAX_VALUE; equals to 4_294_967_294L (0xFFFF_FFFEL)
      See Also:
    • EXPONENT_OF_INFINITY

      public static final int EXPONENT_OF_INFINITY
      The value of the exponent (biased), corresponding to Infinity, -Infinity, and NaN; equals to -1 (0xFFFF_FFFF)
      See Also:
    • ONE

      public static final QuadrupleImmutable ONE
      Value of one.
    • ZERO

      public static final QuadrupleImmutable ZERO
      Value of zero.
    • NEGATIVE_ZERO

      public static final QuadrupleImmutable NEGATIVE_ZERO
      Value of negative zero.
    • POSITIVE_INFINITY

      public static final QuadrupleImmutable POSITIVE_INFINITY
      Value of +Infinity.
    • NEGATIVE_INFINITY

      public static final QuadrupleImmutable NEGATIVE_INFINITY
      Value of -Infinity.
    • NAN

      public static final QuadrupleImmutable NAN
      Value of NaN.
    • PI

      public static final QuadrupleImmutable PI
      Value of PI.
  • Constructor Details

    • QuadrupleImmutable

      public QuadrupleImmutable(int exponent, long mantHi, long mantLo)
      Creates a new QuadrupleImmutable with a positive value built from the given parts.
      Parameters:
      exponent - The binary exponent (unbiased).
      mantHi - The most significant 64 bits of fractional part of the mantissa.
      mantLo - The least significant 64 bits of fractional part of the mantissa.
    • QuadrupleImmutable

      public QuadrupleImmutable(QuadrupleImmutable value)
      Creates a new QuadrupleImmutable instance with the value of the given QuadrupleImmutable instance.
      First creates an empty (zero) instance, then copies the fields of the parameter. to the fields of the new instance
      Parameters:
      value - the QuadrupleImmutable value to be assigned to the new instance.
    • QuadrupleImmutable

      public QuadrupleImmutable(boolean negative, int exponent, long mantHi, long mantLo)
      Creates an instance of a QuadrupleImmutable record class.
      Parameters:
      negative - the value for the negative record component
      exponent - the value for the exponent record component
      mantHi - the value for the mantHi record component
      mantLo - the value for the mantLo record component
  • Method Details

    • infinity

      public static QuadrupleImmutable infinity(boolean negative)
    • zero

      public static QuadrupleImmutable zero(boolean negative)
    • from

      public static QuadrupleImmutable from(double value)
      Creates a new QuadrupleImmutable instance with the given double value.
      Parameters:
      value - the double value to be assigned.
    • from

      public static QuadrupleImmutable from(long value)
      Creates a new QuadrupleImmutable instance with the given long value.
      Parameters:
      value - the long value to be assigned.
    • fromUnbiasedExponent

      public static QuadrupleImmutable fromUnbiasedExponent(boolean negative, int exponent, long mantHi, long mantLo)
      Builds a Float128 value from the given low-level parts.
      Treats the exponent parameter as the unbiased exponent value, whose 0 value corresponds to the QuadrupleImmutable value of 1.0.
      Parameters:
      negative - the sign of the value (true for negative)
      exponent - Binary exponent (unbiased, 0 means 2^0)
      mantHi - The higher 64 bits of the fractional part of the mantissa
      mantLo - The lower 64 bits of the fractional part of the mantissa
      Returns:
      A Float128 containing the value built of the given parts
    • intValue

      public int intValue()
      Converts the value of this QuadrupleImmutable to an int value in a way similar to standard narrowing conversions (e.g., from double to int).
      Returns:
      the value of this QuadrupleImmutable instance converted to an int.
    • longValue

      public long longValue()
      Converts the value of this QuadrupleImmutable to a long value in a way similar to standard narrowing conversions (e.g., from double to long).
      Returns:
      the value of this QuadrupleImmutable instance converted to a long.
    • floatValue

      public float floatValue()
      Converts the value of this QuadrupleImmutable to a float value in a way similar to standard narrowing conversions (e.g., from double to float).
      Returns:
      the value of this QuadrupleImmutable instance converted to a float.
    • doubleValue

      public double doubleValue()
      Converts the value of this QuadrupleImmutable to a double value in a way similar to standard narrowing conversions (e.g., from double to float). Uses 'half-even' approach to the rounding, like BigDecimal.doubleValue()
      Returns:
      the value of this QuadrupleImmutable instance converted to a double.
    • add

      public QuadrupleImmutable add(QuadrupleImmutable summand)
      Adds the value of the given QuadrupleImmutable summand to the value of this Float128. The instance acquires a new value that equals the sum of the previous value and the value of the summand.
      Parameters:
      summand - the value to add
      Returns:
      the reference to this object, which holds a new value that equals the sum of its previous value and the value of the summand
    • add

      public QuadrupleImmutable add(long summand)
      Adds the value of the given long summand to the value of this Float128. The value of the long operand is preliminarily converted to a QuadrupleImmutable value. The instance acquires the new value that equals the sum of the previous value and the value of the summand.
      Parameters:
      summand - the value to add
      Returns:
      the reference to this object, which holds a new value that equals the sum of its previous value and the value of the summand
    • add

      public QuadrupleImmutable add(double summand)
      Adds the value of the given double summand to the value of this Float128. The value of the double operand is preliminarily converted to a QuadrupleImmutable value. The instance acquires the new value that equals the sum of the previous value and the value of the summand.
      Parameters:
      summand - the value to add
      Returns:
      the reference to this object, which holds a new value that equals the sum of its previous value and the value of the summand
    • subtract

      public QuadrupleImmutable subtract(QuadrupleImmutable subtrahend)
      Subtracts the value of the given QuadrupleImmutable subtrahend from the value of this Float128. The instance acquires a new value that equals the difference between the previous value and the value of the subtrahend.
      Parameters:
      subtrahend - the value to be subtracted from the current value of this Float128
      Returns:
      the reference to this object, which holds a new value that equals the difference between its previous value and the value of the subtrahend
    • subtract

      public QuadrupleImmutable subtract(long subtrahend)
      Subtracts the value of the given long subtrahend from the value of this Float128. The value of the long subtrahend is preliminarily converted to a QuadrupleImmutable value. The instance acquires a new value that equals the difference between the previous value and the value of the subtrahend.
      Parameters:
      subtrahend - the value to be subtracted from the current value of this Float128
      Returns:
      the reference to this object, which holds a new value that equals the difference between its previous value and the value of the subtrahend
    • subtract

      public QuadrupleImmutable subtract(double subtrahend)
      Subtracts the value of the given double subtrahend from the value of this Float128. The value of the double subtrahend is preliminarily converted to a QuadrupleImmutable value. The instance acquires a new value that equals the difference between the previous value and the value of the subtrahend.
      Parameters:
      subtrahend - the value to be subtracted from the current value of this Float128
      Returns:
      the reference to this object, which holds a new value that equals the difference between its previous value and the value of the subtrahend
    • multiply

      public QuadrupleImmutable multiply(QuadrupleImmutable factor)
      Multiplies the value of this Float128 by the value of the given QuadrupleImmutable factor. The instance acquires a new value that equals the product of the previous value and the value of the factor.
      Parameters:
      factor - the value to multiply the current value of this Float128 by.
      Returns:
      the reference to this object, which holds a new value that equals the product of its previous value and the value of the factor
    • multiply

      public QuadrupleImmutable multiply(long factor)
      Multiplies the value of this Float128 by the value of the given long factor. The value of the long factor is preliminarily converted to a QuadrupleImmutable value. The instance acquires a new value that equals the product of the previous value and the value of the factor.
      Parameters:
      factor - the value to multiply the current value of this Float128 by.
      Returns:
      the reference to this object, which holds a new value that equals the product of its previous value and the value of the factor
    • multiply

      public QuadrupleImmutable multiply(double factor)
      Multiplies the value of this Float128 by the value of the given double factor. The value of the double factor is preliminarily converted to a QuadrupleImmutable value. The instance acquires a new value that equals the product of the previous value and the value of the factor.
      Parameters:
      factor - the value to multiply the current value of this Float128 by.
      Returns:
      the reference to this object, which holds a new value that equals the product of its previous value and the value of the factor
    • divide

      public QuadrupleImmutable divide(QuadrupleImmutable divisor)
      Divides the value of this Float128 by the value of the given QuadrupleImmutable divisor. The instance acquires a new value that equals the quotient.
      Parameters:
      divisor - the divisor to divide the current value of this Float128 by
      Returns:
      the reference to this object, which holds a new value that equals the quotient of the previous value of this Float128 divided by the given divisor
    • divide

      public QuadrupleImmutable divide(long divisor)
      Divides the value of this Float128 by the value of the given long divisor. The instance acquires a new value that equals the quotient. The value of the long divisor is preliminarily converted to a QuadrupleImmutable value.
      Parameters:
      divisor - the divisor to divide the current value of this Float128 by
      Returns:
      the reference to this object, which holds a new value that equals the quotient of the previous value of this Float128 divided by the given divisor
    • divide

      public QuadrupleImmutable divide(double divisor)
      Divides the value of this Float128 by the value of the given double divisor. The instance acquires a new value that equals the quotient. The value of the double divisor is preliminarily converted to a QuadrupleImmutable value.
      Parameters:
      divisor - the divisor to divide the current value of this Float128 by
      Returns:
      the reference to this object, which holds a new value that equals the quotient of the previous value of this Float128 divided by the given divisor
    • sqrt

      public QuadrupleImmutable sqrt()
      Computes a square root of the value of this QuadrupleImmutable and replaces the old value of this instance with the newly-computed value.
      Returns:
      the reference to this instance, which holds a new value that equals to the square root of its previous value
    • sqrt

      public static QuadrupleImmutable sqrt(QuadrupleImmutable square)
      Computes a square root of the value of the given QuadrupleImmutable, creates and returns a new instance of Float128 containing the value of the square root. The parameter remains unchanged.
      Parameters:
      square - the value to find the square root of
      Returns:
      a new instance of Float128 containing the value of the square root of the given argument
    • cpy

      public QuadrupleImmutable cpy()
      Returns a copy of this Float128.
      Returns:
      A new instance of Float128 with the same value as this
    • abs

      public QuadrupleImmutable abs()
      Returns a new instance of QuadrupleImmutable with the value of the absolute value of this instance
      Returns:
      a new instance of QuadrupleImmutable with the value of the absolute value of this instance
    • isNegative

      public boolean isNegative()
      Checks if the value is negative.
      Returns:
      true, if the value is negative, false otherwise
    • isInfinite

      public boolean isInfinite()
      Checks if the value is infinite (i.e NEGATIVE_INFINITY or POSITIVE_INFINITY).
      Returns:
      true, if the value is infinity (either positive or negative), false otherwise
    • isNaN

      public boolean isNaN()
      Checks if the value is not a number (i.e. has the value of NaN).
      Returns:
      true, if the value is not a number (NaN), false otherwise
    • isZero

      public boolean isZero()
      Checks if the value is zero, either positive or negative.
      Returns:
      true, if the value is 0 or -0, otherwise returns
    • signum

      public int signum()
      Returns 1 for positive values, -1 for negative values (including -0), and 0 for the positive zero value
      Returns:
      1 for positive values, -1 for negative values (including -0), and 0 for the positive zero value
    • compareTo

      public int compareTo(QuadrupleImmutable other)
      Compares the value of this instance with the value of the specified instance.
      Specified by:
      compareTo in interface Comparable<QuadrupleImmutable>
      Parameters:
      other - the QuadrupleImmutable to compare with
      Returns:
      a negative integer, zero, or a positive integer as the value of this instance is less than, equal to, or greater than the value of the specified instance.
    • hashCode

      public int hashCode()
      Computes a hashcode for this QuadrupleImmutable, based on the values of its fields.
      Specified by:
      hashCode in class Record
      See Also:
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • negative

      public boolean negative()
      Returns the value of the negative record component.
      Returns:
      the value of the negative record component
    • exponent

      public int exponent()
      Returns the value of the exponent record component.
      Returns:
      the value of the exponent record component
    • mantHi

      public long mantHi()
      Returns the value of the mantHi record component.
      Returns:
      the value of the mantHi record component
    • mantLo

      public long mantLo()
      Returns the value of the mantLo record component.
      Returns:
      the value of the mantLo record component