Class BCDFloat

  • All Implemented Interfaces:
    Cloneable, Comparable<Number>, Number

    public final class BCDFloat
    extends Object
    implements Number
    Arbitrary Precision Denormalized Floating Point Arithmetic.

    For the use in an FX-602P Simulator the precision is fixed at 14 decimal digits.

    Note: Local variables are used in trace, and x and y variables are sometimes used inverted to the parameters declared.

    • Field Detail

      • Logger

        private static final Logger Logger

        Class logger instance.

      • Max_Exponent

        private static final int Max_Exponent
        Maximum supported Exponent + 1
        See Also:
        Constant Field Values
      • Num_0_5_π

        @NotNull
        private static final @NotNull BCDFloat Num_0_5_π
        0.5 π (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • Num_10

        @NotNull
        public static final @NotNull BCDFloat Num_10
        10
      • Num_1_div_e

        @NotNull
        private static final @NotNull BCDFloat Num_1_div_e
        1 divided by Euler's number (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • Num_2_π

        @NotNull
        private static final @NotNull BCDFloat Num_2_π
        2 π (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • Num_log_e_10

        @NotNull
        private static final @NotNull BCDFloat Num_log_e_10
        Loge10 (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • TAG

        private static final String TAG

        Class logger tag.

      • NaN

        @NotNull
        public static final @NotNull BCDFloat NaN
        Not a number
      • Num_0

        @NotNull
        public static final @NotNull BCDFloat Num_0
        0
      • Num_0_5

        @NotNull
        public static final @NotNull BCDFloat Num_0_5
        0.5
      • Num_1

        @NotNull
        public static final @NotNull BCDFloat Num_1
        1
      • Num_100

        @NotNull
        public static final @NotNull BCDFloat Num_100
        100, for example used for percent
      • Num_1000

        @NotNull
        public static final @NotNull BCDFloat Num_1000
        100, for example used for percent
      • Num_180

        @NotNull
        public static final @NotNull BCDFloat Num_180
        180, for example used for half a circle in degree
      • Num_2

        @NotNull
        public static final @NotNull BCDFloat Num_2
        2
      • Num_200

        @NotNull
        public static final @NotNull BCDFloat Num_200
        200, for example used for half a circle in radiant.
      • Num_3600

        @NotNull
        public static final @NotNull BCDFloat Num_3600
        60, For example used for seconds in an hour.
      • Num_60

        @NotNull
        public static final @NotNull BCDFloat Num_60
        60, For example used for minutes in an hour.
      • Num_Minus_1

        @NotNull
        public static final @NotNull BCDFloat Num_Minus_1
        -1
      • Num_e

        @NotNull
        public static final @NotNull BCDFloat Num_e
        Euler's number (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • Num_π

        @NotNull
        public static final @NotNull BCDFloat Num_π
        π (18 digits)

        Unlike the original BCDFloat from Kai G. Schwebke I use a fixed maximum precision defined at compile time - so there is no need for truly arbitrary irrational constants.

      • Coefficient

        @NotNull
        private @NotNull BCDInteger Coefficient

        coefficient - while a 18 digits number can also be represented as an long an 18digits multiplication need a 36digits temporary and that is more then a long can do.

        null for NaN and infinities

      • Exponent

        private int Exponent
        current exponent - note that the exponent is based on the internal representation where the radix point is right most. For example π is 314159265358979324×10-17.
      • Type

        @NotNull
        private @NotNull BCDFloat.Float_Type Type
        Numeric Type
        Finite
        Normal Real Number
        NaN
        Not a Number
        Type_Positive_Infinity
        +∞dd>
        Type_Negative_Infinity
        -∞
    • Constructor Detail

      • BCDFloat

        public BCDFloat()
        Create empty value
      • BCDFloat

        public BCDFloat​(@NotNull
                        @NotNull BCDFloat Value)
        create new value from existing one.
        Parameters:
        Value - value to init the instance from
      • BCDFloat

        public BCDFloat​(@NotNull
                        @NotNull BCDInteger Value)
        create new value from existing one.
        Parameters:
        Value - value to init the instance from
      • BCDFloat

        public BCDFloat​(long Value)
        create from java long
        Parameters:
        Value - value to init the instance from
      • BCDFloat

        public BCDFloat​(long Coefficient,
                        int Exponent)
        create from Coefficient and Exponent
        Parameters:
        Coefficient - Coefficient
        Exponent - Exponent
      • BCDFloat

        public BCDFloat​(String Value)
        create from string
        Parameters:
        Value - string to create float form
    • Method Detail

      • $div

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number $div​(@NotNull
                                          @NotNull Number Y)
        x ÷ y
        Specified by:
        $div in interface Number
        Parameters:
        Y - divisor
        Returns:
        x ÷ y
      • $minus

        @Contract(pure=true)
        @NotNull
        public @NotNull Number $minus​(@NotNull
                                      @NotNull Number Y)
        x - y
        Specified by:
        $minus in interface Number
        Parameters:
        Y - value to subtract
        Returns:
        x - y
      • $plus

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number $plus​(@NotNull
                                           @NotNull Number Y)
        x + Y
        Specified by:
        $plus in interface Number
        Parameters:
        Y - value to add
        Returns:
        x + Y
      • $times

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number $times​(@NotNull
                                            @NotNull Number Y)
        x × Y
        Specified by:
        $times in interface Number
        Parameters:
        Y - Multiplier
        Returns:
        x × Y
      • $times$times

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number $times$times​(@NotNull
                                                  @NotNull Number Y)
                                           throws BCDError
        xY
        Specified by:
        $times$times in interface Number
        Parameters:
        Y - power
        Returns:
        xy
        Throws:
        BCDError - calculation error
      • As_BCDFloat

        @NotNull
        public @NotNull BCDFloat As_BCDFloat()

        Either convert to a BCDFloat or return this.

        Specified by:
        As_BCDFloat in interface Number
        Returns:
        value as java int
      • As_BCDFraction

        @NotNull
        public @NotNull BCDFraction As_BCDFraction()

        Either convert to a or return this.

        Specified by:
        As_BCDFraction in interface Number
        Returns:
        value as java int
      • As_BCDInteger

        @NotNull
        public @NotNull BCDInteger As_BCDInteger()

        Either convert to a BCDInteger or return this.

        Specified by:
        As_BCDInteger in interface Number
        Returns:
        value as java int
      • Clone

        @NotNull
        public @NotNull Number Clone()
        clone the float - but it might be easier to use the provided copy Constructor instead.
        Specified by:
        Clone in interface Number
        Returns:
        a new BCDFloat from an existing one.
        See Also:
        Object.clone()
      • Coefficient

        @NotNull
        public @NotNull BCDInteger Coefficient()

        coefficient - while a 18 digits number can also be represented as an long an 18digits multiplication need a 36digits temporary and that is more then a long can do.

        Returns:
        Coefficient
      • Coefficient_As_Long

        public long Coefficient_As_Long()
                                 throws BCDError

        coefficient - while a 18 digits number can also be represented as an long an 18digits multiplication need a 36digits temporary and that is more then a long can do.

        Specified by:
        Coefficient_As_Long in interface Number
        Returns:
        Coefficient
        Throws:
        BCDError - when it's not a normal number
      • Compare

        public final int Compare​(@NotNull
                                 @NotNull Number Value)
                          throws BCDError
        Compare values
        Specified by:
        Compare in interface Number
        Parameters:
        Value - value to compare with
        Returns:
        Compare_Equal
        Values are the same
        Compare_Less
        Left value less the right value
        Compare_Greater
        Left value greater the right value
        Throws:
        BCDError - numbers can not be compared.
      • Digits

        public byte[] Digits()
        number: sum(i = 0 ... Num_Digits-1; Digits[i]*10^i)
        Specified by:
        Digits in interface Number
      • Exponent

        public final int Exponent()
                           throws BCDError
        current exponent - note that the exponent is based on the internal representation where the radix point is right most. For example π is 314159265358979324×10-17.
        Specified by:
        Exponent in interface Number
        Returns:
        Current exponent.
        Throws:
        BCDError - when the value is not finite
      • Fix

        @NotNull
        public final @NotNull BCDFloat Fix​(int Decimal,
                                           int Exponent)
        Round to a given Precision after the decimal point.
        Specified by:
        Fix in interface Number
        Parameters:
        Decimal - decimal digits to keep
        Exponent - max exponent
        Returns:
        rounded value
      • Frac

        @NotNull
        public final @NotNull BCDFloat Frac()
        Recompose a frac double from the given data.
        Specified by:
        Frac in interface Number
        Returns:
        a double
      • Get_1_Normalized_Exponent

        public final int Get_1_Normalized_Exponent()
        current exponent so that the ratix point is 1 digit to right. For example π would be 3.14159265358979324×100.
        Returns:
        Current exponent.
      • Indirect_Value

        public int Indirect_Value​(int Digit_Count)

        Get valuef or indirect addressing.

        Specified by:
        Indirect_Value in interface Number
        Parameters:
        Digit_Count - Should be 1 … 2 depending if value is used to access memory or perform a goto.
        Returns:
        int value for indirect addressing.
      • Integer

        @NotNull
        public @NotNull Number Integer()
        integer part of the BCDFloat

        Do not mix up with To_Integer - This version returns another BCDFloat!

        Specified by:
        Integer in interface Number
        Returns:
        integer part of this
      • Is_Finite

        public final boolean Is_Finite()
        current value is a normal number
        Specified by:
        Is_Finite in interface Number
        Returns:
        true when it is a normal number
      • Is_Infinite

        public final boolean Is_Infinite()
        current value is Infinity (but not N/A).
        Specified by:
        Is_Infinite in interface Number
        Returns:
        true when x is infinite
      • Is_Integer

        public boolean Is_Integer()
        Current value is integer value.
        Specified by:
        Is_Integer in interface Number
        Returns:
        true when x is an integer number
      • Is_NaN

        public final boolean Is_NaN()
        current value is not a number
        Specified by:
        Is_NaN in interface Number
        Returns:
        when is not a number
      • Is_Negative

        public final boolean Is_Negative()
        Current value us negative
        Specified by:
        Is_Negative in interface Number
        Returns:
        true when x <= -0
      • Is_Zero

        public final boolean Is_Zero()
        value is 0
        Specified by:
        Is_Zero in interface Number
        Returns:
        true when x is 0
      • Normalize

        public final void Normalize()

        normalize the value

        Specified by:
        Normalize in interface Number
      • Normalize

        private void Normalize​(int maxE)
        Parameters:
        maxE - maximum exponent
      • Num_Digits

        public int Num_Digits()
                       throws BCDError
        the current number of digits - not that when the number is not normalised there might be more 0's in the number then strictly needed.
        Specified by:
        Num_Digits in interface Number
        Returns:
        number of digits
        Throws:
        BCDError - Not a finite number.
      • P_To_X

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number P_To_X​(@NotNull
                                            @NotNull Number θ)
        Convert Polar to Rectangle Coordinates
        Specified by:
        P_To_X in interface Number
        Parameters:
        \u03b8 - angle
        Returns:
        x = cos (w) × r
      • P_To_X

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number P_To_X​(@NotNull
                                            @NotNull Number θ,
                                            @NotNull
                                            @NotNull Number Half_Circle)
        Convert Polar to Rectangle Coordinates
        Specified by:
        P_To_X in interface Number
        Parameters:
        \u03b8 - angle
        Half_Circle - 180 for deg, 200 for gra, pi for rad
        Returns:
        x = cos (w) × r
      • P_To_Y

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number P_To_Y​(@NotNull
                                            @NotNull Number θ)
        Convert Polar to Rectangle Coordinates
        Specified by:
        P_To_Y in interface Number
        Parameters:
        \u03b8 - angle
        Returns:
        y = sin (θ) × r
      • P_To_Y

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number P_To_Y​(@NotNull
                                            @NotNull Number θ,
                                            @NotNull
                                            @NotNull Number Half_Circle)
        Convert Polar to Rectangle Coordinates
        Specified by:
        P_To_Y in interface Number
        Parameters:
        \u03b8 - angle
        Half_Circle - 180 for deg, 200 for gra, pi for rad
        Returns:
        y = sin (θ) × r
      • R_To_R

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number R_To_R​(@NotNull
                                            @NotNull Number y)
        Convert Rectangle to Polar Coordinates
        Specified by:
        R_To_R in interface Number
        Parameters:
        y - y position
        Returns:
        r = √(x²+y²)
      • R_To_θ

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number R_To_θ​(@NotNull
                                            @NotNull Number y)
        Convert Rectangle to Polar Coordinates
        Specified by:
        R_To_θ in interface Number
        Parameters:
        y - y position
        Returns:
        θ = arctan (y÷x)
      • R_To_θ

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number R_To_θ​(@NotNull
                                            @NotNull Number y,
                                            @NotNull
                                            @NotNull Number Half_Circle)
        Convert Rectangle to Polar Coordinates
        Specified by:
        R_To_θ in interface Number
        Parameters:
        y - y position
        Half_Circle - 180 for deg, 200 for gra, pi for rad
        Returns:
        θ = arctan (y÷x)
      • Round

        @NotNull
        public @NotNull Number Round()
        Round to Effective and Max_Exponent.
        Specified by:
        Round in interface Number
        Returns:
        rounded value
      • Round

        @NotNull
        public final @NotNull Number Round​(int Precision,
                                           int Exponent)
        Round to a given Precision and Exponent.
        Specified by:
        Round in interface Number
        Parameters:
        Precision - Total precison
        Exponent - max exponent
        Returns:
        rounded value
      • Round_Effective

        private void Round_Effective()
        Round to Effective and Max_Exponent.
      • Round_Max

        private void Round_Max()
        Round to Max_Precision and Max_Exponent.
      • Round_To

        private void Round_To​(int Precision,
                              int Exponent)
        Round to a given Precision and Exponent.
        Parameters:
        Precision - Total precision
        Exponent - max exponent
      • To_Debug_String

        @NotNull
        public @NotNull String To_Debug_String()
        convert to native/debug string representation
        Specified by:
        To_Debug_String in interface Number
        Returns:
        (Coefficient, Exponent)
      • To_Integer

        public int To_Integer()
        Convert to integer

        Do not mix up with Integer - This version returns a Java int!

        Specified by:
        To_Integer in interface Number
        Returns:
        value as java int
      • To_Long

        public long To_Long()

        Convert to long integer

        Do not mix up with Integer - This version returns a Java int!

        Specified by:
        To_Long in interface Number
        Returns:
        value as java int
      • abs

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number abs()
        |x|
        Specified by:
        abs in interface Number
        Returns:
        abs value
      • and

        @Contract(pure=true)
        @NotNull
        public @NotNull Number and​(@NotNull
                                   @NotNull Number y)

        Logical and

        Specified by:
        and in interface Number
        Returns:
        x ∧ Y
      • arc_cos

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_cos()
        arc cosine
        Specified by:
        arc_cos in interface Number
        Returns:
        arc cosine
      • arc_cos

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_cos​(@NotNull
                                             @NotNull Number Half_Circle)
        arc cosine
        Specified by:
        arc_cos in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        arccos (x) ÷ π × Half_Circle
      • arc_cos_hyp

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_cos_hyp()
        area hyperbolic cosine: loge (x + √(x-1) × √(x+1))
        Specified by:
        arc_cos_hyp in interface Number
        Returns:
        Hyperbolic sine.
      • arc_sin

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_sin()
        arc sine
        Specified by:
        arc_sin in interface Number
        Returns:
        arc sine
      • arc_sin

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_sin​(@NotNull
                                             @NotNull Number Half_Circle)
        arc sine
        Specified by:
        arc_sin in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        arcsin (x) ÷ π × Half_Circle
      • arc_sin_hyp

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_sin_hyp()
        area hyperbolic sine: loge (x + √(x² + 1))
        Specified by:
        arc_sin_hyp in interface Number
        Returns:
        Hyperbolic sine.
      • arc_tan

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_tan()
        arc tangent
        Specified by:
        arc_tan in interface Number
        Returns:
        arc tangent
      • arc_tan

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_tan​(@NotNull
                                             @NotNull Number Half_Circle)
        arc tangent
        Specified by:
        arc_tan in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        arctan (x) ÷ π × Half_Circle
      • arc_tan_hyp

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number arc_tan_hyp()
        area hyperbolic tangent: loge ((1 + x) ÷ (1 - x)) / 2
        Specified by:
        arc_tan_hyp in interface Number
        Returns:
        Hyperbolic sine.
      • compareTo

        public int compareTo​(@NotNull
                             @NotNull Number rightValue)
        Specified by:
        compareTo in interface Comparable<Number>
        Specified by:
        compareTo in interface Number
        Parameters:
        rightValue - value to compare with. (no BCDFloat exception thrown)
        Returns:
        Compare_Equal
        Values are the same
        Compare_Less
        Left value less the right value
        Compare_Greater
        Left value greater the right value
        See Also:
        Comparable.compareTo(java.lang.Object)
      • cos

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number cos()
        cos (x)
        Specified by:
        cos in interface Number
        Returns:
        cos (x)
      • cos

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number cos​(@NotNull
                                         @NotNull Number Half_Circle)
        cosine
        Specified by:
        cos in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        sin (x × π ÷ Half_Circle)
      • cos_hyp

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number cos_hyp()
        Hyperbolic cosine: (Exponent^x + Exponent^-x)/2
        Specified by:
        cos_hyp in interface Number
        Returns:
        Hyperbolic cosine.
      • equals

        public boolean equals​(@NotNull
                              @NotNull Object rightValue)
        Specified by:
        equals in interface Number
        Overrides:
        equals in class Object
        Parameters:
        rightValue - BCDFloat to compare with
        Returns:
        true when the values are equal
      • expI

        @NotNull
        private @NotNull BCDFloat expI()
        Exponent^x using series expansion, usable around 0
        Returns:
        ex
      • exp_10

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number exp_10()
                                     throws BCDError
        10x
        Specified by:
        exp_10 in interface Number
        Returns:
        10x
        Throws:
        BCDError - calculation error
      • exp_e

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number exp_e()
        ex
        Specified by:
        exp_e in interface Number
        Returns:
        ex
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
        Returns:
        a has code
      • lnI

        @NotNull
        private @NotNull Number lnI()
        loge x using series expansion, usable around 1
        Returns:
        loge
      • log_10

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number log_10()
        log10 (x)
        Specified by:
        log_10 in interface Number
        Returns:
        log10 (x)
      • log_e

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number log_e()
        loge (x)
        Specified by:
        log_e in interface Number
        Returns:
        loge (x)
      • neg

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number neg()
        Specified by:
        neg in interface Number
        Returns:
        negative value
      • not

        @Contract(pure=true)
        @NotNull
        public @NotNull Number not()

        Logical not

        Specified by:
        not in interface Number
        Returns:
        ¬ x
      • or

        @Contract(pure=true)
        @NotNull
        public @NotNull Number or​(@NotNull
                                  @NotNull Number y)

        Logical or

        Specified by:
        or in interface Number
        Parameters:
        y - right value
        Returns:
        x ∨ y
      • root

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number root​(@NotNull
                                          @NotNull Number r)
        r√x
        Specified by:
        root in interface Number
        Parameters:
        r - root
        Returns:
        e(loge x ÷ r)
      • sin

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number sin()
        sine
        Specified by:
        sin in interface Number
        Returns:
        sin (x)
      • sin

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number sin​(@NotNull
                                         @NotNull Number Half_Circle)
        sine
        Specified by:
        sin in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        sin (x × π ÷ Half_Circle)
      • sinI

        @NotNull
        private @NotNull Number sinI()
        sin using series expansion, usable around 0 (-π .. π)
        Returns:
        sin (x)
      • sin_hyp

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number sin_hyp()
        hyperbolic sine: (ex - e-x) ÷ 2
        Specified by:
        sin_hyp in interface Number
        Returns:
        Hyperbolic sine.
      • square

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number square()
        x2
        Specified by:
        square in interface Number
        Returns:
        x2
      • square_root

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number square_root()
        2√x
        Specified by:
        square_root in interface Number
        Returns:
        2√x
      • tan

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number tan()
        tan x
        Specified by:
        tan in interface Number
        Returns:
        tan (x × π ÷ Half_Circle)
      • tan

        @Contract(pure=true)
        @NotNull
        public final @NotNull Number tan​(@NotNull
                                         @NotNull Number Half_Circle)
        tangent
        Specified by:
        tan in interface Number
        Parameters:
        Half_Circle - value for a half circle
        Returns:
        tangent
      • tan_hyp

        @NotNull
        public final @NotNull Number tan_hyp()
        Hyperbolic tangent: (e2x - 1) ÷ (e2x + 1)
        Specified by:
        tan_hyp in interface Number
        Returns:
        Hyperbolic tangent.
      • xor

        @Contract(pure=true)
        @NotNull
        public @NotNull Number xor​(@NotNull
                                   @NotNull Number y)

        Logical xor

        Specified by:
        xor in interface Number
        Returns:
        x ⊻ y