Java BigInteger Calculate decodeFloat(BigInteger rawSign, BigInteger rawExponent, BigInteger rawMantissa, int signWidth, int exponentWidth, int mantissaWidth, int bias, MathContext mc)

Here you can find the source of decodeFloat(BigInteger rawSign, BigInteger rawExponent, BigInteger rawMantissa, int signWidth, int exponentWidth, int mantissaWidth, int bias, MathContext mc)

Description

decode Float

License

Mozilla Public License

Declaration

public static Number decodeFloat(BigInteger rawSign,
            BigInteger rawExponent, BigInteger rawMantissa, int signWidth,
            int exponentWidth, int mantissaWidth, int bias, MathContext mc) 

Method Source Code

//package com.java2s;
/*/*from   w  w w. j  a  va  2s  . c  o  m*/
 * Copyright © 2010-2011 Rebecca G. Bettencourt / Kreative Software
 * <p>
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * <a href="http://www.mozilla.org/MPL/">http://www.mozilla.org/MPL/</a>
 * <p>
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * <p>
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License (the "LGPL License"), in which
 * case the provisions of LGPL License are applicable instead of those
 * above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL License and not to allow others to use
 * your version of this file under the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and
 * other provisions required by the LGPL License. If you do not delete
 * the provisions above, a recipient may use your version of this file
 * under either the MPL or the LGPL License.
 * @since KSFL 1.2
 * @author Rebecca G. Bettencourt, Kreative Software
 */

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

public class Main {
    public static Number decodeFloat(BigInteger rawSign,
            BigInteger rawExponent, BigInteger rawMantissa, int signWidth,
            int exponentWidth, int mantissaWidth, int bias, MathContext mc) {
        if (signWidth < 0 || signWidth > 1 || exponentWidth < 0
                || mantissaWidth < 0)
            throw new IllegalArgumentException();
        else if (rawExponent.compareTo(BigInteger.ZERO) == 0) {
            // zero or subnormal
            if (rawMantissa.compareTo(BigInteger.ZERO) == 0) {
                // zero
                // must use double, instead of BigDecimal, in order to preserve sign
                boolean isNegative = (rawSign.compareTo(BigInteger.ZERO) != 0);
                return isNegative ? -0.0 : 0.0;
            } else {
                // subnormal
                boolean isNegative = (rawSign.compareTo(BigInteger.ZERO) != 0);
                int negativeExponent = bias + mantissaWidth - 1;
                BigDecimal mantissa = new BigDecimal(rawMantissa);
                if (negativeExponent < 0) {
                    BigDecimal multiplier = BigDecimal.valueOf(2L).pow(
                            -negativeExponent);
                    return isNegative ? mantissa.multiply(multiplier, mc)
                            .negate() : mantissa.multiply(multiplier, mc);
                } else {
                    BigDecimal multiplier = BigDecimal.valueOf(2L).pow(
                            negativeExponent);
                    return isNegative ? mantissa.divide(multiplier, mc)
                            .negate() : mantissa.divide(multiplier, mc);
                }
            }
        } else if (rawExponent.compareTo(BigInteger.ONE.shiftLeft(
                exponentWidth).subtract(BigInteger.ONE)) == 0) {
            // infinity or NaN
            if (rawMantissa.compareTo(BigInteger.ZERO) == 0) {
                // infinity
                boolean isNegative = (rawSign.compareTo(BigInteger.ZERO) != 0);
                return isNegative ? Double.NEGATIVE_INFINITY
                        : Double.POSITIVE_INFINITY;
            } else {
                // NaN
                boolean isNegative = (rawSign.compareTo(BigInteger.ZERO) != 0);
                boolean isQuietNaN = rawMantissa.testBit(mantissaWidth - 1);
                BigInteger mantissa = rawMantissa
                        .clearBit(mantissaWidth - 1);
                long rawDouble = 0x7FF0000000000000L;
                if (isNegative)
                    rawDouble |= 0x8000000000000000L;
                if (isQuietNaN)
                    rawDouble |= 0x0008000000000000L;
                rawDouble |= (mantissa.longValue() & 0x0007FFFFFFFFFFFFL);
                return Double.longBitsToDouble(rawDouble);
            }
        } else {
            // normal
            boolean isNegative = (rawSign.compareTo(BigInteger.ZERO) != 0);
            int negativeExponent = bias + mantissaWidth
                    - rawExponent.intValue();
            BigDecimal mantissa = new BigDecimal(
                    rawMantissa.setBit(mantissaWidth));
            if (negativeExponent < 0) {
                BigDecimal multiplier = BigDecimal.valueOf(2L).pow(
                        -negativeExponent);
                return isNegative ? mantissa.multiply(multiplier, mc)
                        .negate() : mantissa.multiply(multiplier, mc);
            } else {
                BigDecimal multiplier = BigDecimal.valueOf(2L).pow(
                        negativeExponent);
                return isNegative ? mantissa.divide(multiplier, mc)
                        .negate() : mantissa.divide(multiplier, mc);
            }
        }
    }
}

Related

  1. createUnsignedBigInteger(byte[] data)
  2. createUUID(int version, String gtin, BigInteger phar)
  3. cryptRSA(byte[] data, BigInteger exponent, BigInteger modulus)
  4. decodeBigInteger(String value)
  5. decodeBitListFromBigInteger(BigInteger bits)
  6. decodeToBigInteger(String input)
  7. DiffieHellman(BigInteger p, BigInteger g, BigInteger x, BigInteger y, BigInteger y_B)
  8. distance(BigInteger a, BigInteger b)
  9. distance(BigInteger a, BigInteger b)