ieee754.java~1~

来自「一个一元曲线多项式数值演示例子」· JAVA~1~ 代码 · 共 602 行 · 第 1/2 页

JAVA~1~
602
字号

    /**
     * Print the decomposed parts of the value.
     */
    public void print()
    {
        System.out.println("------------------------------");

        // Print the value.
        if (isDouble()) {
            System.out.println("double value = " + doubleValue());
        }
        else {
            System.out.println("float value = " + floatValue());
        }

        // Print the sign.
        System.out.print("sign=" + signBit());

        // Print the bit representation of the exponent and its
        // biased and unbiased values.  Indicate whether the value
        // is denormalized, or whether the exponent is reserved.
        System.out.print(", exponent=" + exponentBits() +
                         " (biased=" + biasedExponent());

        if (isZero()) {
            System.out.println(", zero)");
        }
        else if (isExponentReserved()) {
            System.out.println(", reserved)");
        }
        else if (isDenormalized()) {
            System.out.println(", denormalized, use " +
                               unbiasedExponent() + ")");
        }
        else {
            System.out.println(", normalized, unbiased=" +
                               unbiasedExponent() + ")");
        }

        // Print the significand.
        System.out.println("significand=" + significandBits());
    }

    //--------------------------------//
    // Compute and validate exponents //
    //--------------------------------//

    /**
     * Compute the value of the float biased exponent
     * given the unbiased value.
     * @param unbiased the unbiased exponent value
     * @return the biased exponent value
     */
    public static int toFloatBiasedExponent(int unbiased)
    {
        return unbiased + FLOAT_EXPONENT_BIAS;
    }

    /**
     * Compute the value of the float unbiased exponent
     * given the biased value.
     * @param biased the biased exponent value
     * @return the unbiased exponent value
     */
    public static int toFloatUnbiasedExponent(int biased)
    {
        return biased == 0 ? -FLOAT_EXPONENT_BIAS + 1
                           : biased - FLOAT_EXPONENT_BIAS;
    }

    /**
     * Compute the value of the double biased exponent
     * given the unbiased value.
     * @param unbiased the unbiased exponent value
     * @return the biased exponent value
     */
    public static int toDoubleBiasedExponent(int unbiased)
    {
        return unbiased + DOUBLE_EXPONENT_BIAS;
    }

    /**
     * Compute the value of the double unbiased exponent
     * given the biased value.
     * @param biased the biased exponent value
     * @return the unbiased exponent value
     */
    public static int toDoubleUnbiasedExponent(int biased)
    {
        return biased == 0 ? -DOUBLE_EXPONENT_BIAS + 1
                           : biased - DOUBLE_EXPONENT_BIAS;
    }

    /**
     * Validate the value of the float biased exponent value.
     * @param biased the biased exponent value
     * @throws numbercruncher.mathutils.IEEE754.Exception
     */
    public static void validateFloatBiasedExponent(int biased)
        throws Exception
    {
        if ((biased < 0) ||
            (biased > FLOAT_EXPONENT_RESERVED)) {
            throw new Exception("The biased exponent value should be " +
                                "0 through " + FLOAT_EXPONENT_RESERVED +
                                ".");
        }
    }

    /**
     * Validate the value of the float unbiased exponent value.
     * @param biased the unbiased exponent value
     * @throws numbercruncher.mathutils.IEEE754.Exception
     */
    public static void validateFloatUnbiasedExponent(int unbiased)
        throws Exception
    {
        if ((unbiased < -FLOAT_EXPONENT_BIAS + 1) ||
            (unbiased > FLOAT_EXPONENT_BIAS)) {
            throw new Exception("The unbiased exponent value should be " +
                                -(FLOAT_EXPONENT_BIAS - 1) + " through " +
                                FLOAT_EXPONENT_BIAS + ".");
        }
    }

    /**
     * Validate the value of the double biased exponent value.
     * @param biased the biased exponent value
     * @throws numbercruncher.mathutils.IEEE754.Exception
     */
    public static void validateDoubleBiasedExponent(int biased)
        throws Exception
    {
        if ((biased < 0) ||
            (biased > DOUBLE_EXPONENT_RESERVED)) {
            throw new Exception("The biased exponent value should be " +
                                "0 through " +
                                DOUBLE_EXPONENT_RESERVED + ".");
        }
    }

    /**
     * Validate the value of the double unbiased exponent value.
     * @param biased the unbiased exponent value
     * @throws numbercruncher.mathutils.IEEE754.Exception
     */
    public static void validateDoubleUnbiasedExponent(int unbiased)
        throws Exception
    {
        if ((unbiased < -DOUBLE_EXPONENT_BIAS + 1) ||
            (unbiased > DOUBLE_EXPONENT_BIAS)) {
            throw new Exception("The unbiased exponent value should be " +
                                -(DOUBLE_EXPONENT_BIAS - 1) +
                                " through " +
                                DOUBLE_EXPONENT_BIAS + ".");
        }
    }

    //------------------------------//
    // Nested decomposition classes //
    //------------------------------//

    /**
     * IEEE 754 exception.
     */
    public static class Exception extends java.lang.Exception
    {
        public Exception(String message) { super(message); }
    }

    /**
     * Abstract base class for the IEEE 754 part classes.
     */
    private static abstract class Part
    {
        /** the part buffer */  private StringBuffer part;

        /**
         * Constructor.
         * @param size the bit size of the part
         * @param bits the string of character bits '0' and '1'
         * @throws numbercruncher.mathutils.IEEE754.Exception
         */
        private Part(int size, String bits) throws Exception
        {
            if (size <= 0) {
                throw new Exception("Invalid part size: " + part);
            }

            int length = bits.length();
            part = new StringBuffer(size);

            // String length matches part size.
            if (length == size) {
                part.append(bits);
                validate();
            }

            // String length < part size:  Pad with '0'.
            else if (length < size) {
                part.append(bits);
                validate();
                for (int i = length; i < size; ++i) part.append('0');
            }

            // String length > part size:  Truncate at the right end.
            else {
                part.append(bits.substring(0, size));
                validate();
            }
        }

        /**
         * Convert the part to an integer value.
         * @return the integer value
         * @throws numbercruncher.mathutils.IEEE754.Exception if the
         *         binary number format is invalid
         */
        protected int toInt() throws Exception
        {
            try {
                return Integer.parseInt(part.toString(), 2);
            }
            catch(NumberFormatException ex) {
                throw new Exception("Invalid binary number format: " +
                                    part.toString());
            }
        }

        /**
         * Convert the part to an long value.
         * @return the long value
         * @throws numbercruncher.mathutils.IEEE754.Exception if the
         *         binary number format is invalid
         */
        protected long toLong() throws Exception
        {
            try {
                return Long.parseLong(part.toString(), 2);
            }
            catch(NumberFormatException ex) {
                throw new Exception("Invalid binary number format: " +
                                    part.toString());
            }
        }

        /**
         * Return the part as a string of characters '0' and '1'.
         */
        public String toString() { return part.toString(); }

        /**
         * Validate that the part consists only of '0' and '1'.
         * @throws numbercruncher.mathutils.IEEE754.Exception
         */
        private void validate() throws Exception
        {
            int length = part.length();

            for (int i = 0; i < length; ++i) {
                char bit = part.charAt(i);
                if ((bit != '0') && (bit != '1')) {
                    throw new Exception("Invalid fraction bit string.");
                }
            }
        }
    }

    /**
     * The IEEE 754 fraction part for a float.
     */
    public static class FloatFraction extends Part
    {
        /**
         * Constructor.
         * @param bits the string of character bits '0' and '1'
         * @throws numbercruncher.mathutils.IEEE754.Exception
         */
        public FloatFraction(String bits) throws Exception
        {
            super(FLOAT_FRACTION_SIZE, bits);
        }
    }

    /**
     * The IEEE 754 fraction part for a double.
     */
    public static class DoubleFraction extends Part
    {
        /**
         * Constructor.
         * @param bits the string of character bits '0' and '1'
         * @throws numbercruncher.mathutils.IEEE754.Exception
         */
        public DoubleFraction(String bits) throws Exception
        {
            super(DOUBLE_FRACTION_SIZE, bits);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?