Example usage for java.lang Double doubleToRawLongBits

List of usage examples for java.lang Double doubleToRawLongBits

Introduction

In this page you can find the example usage for java.lang Double doubleToRawLongBits.

Prototype

@HotSpotIntrinsicCandidate
public static native long doubleToRawLongBits(double value);

Source Link

Document

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.

Usage

From source file:Main.java

public static void main(String[] args) {

    Double d = new Double("10.50");

    System.out.println("Value = " + d.doubleToRawLongBits(2.5d));
}

From source file:Main.java

public static byte[] doubleToByte(double d) {
    byte[] b = new byte[8];
    long l = Double.doubleToRawLongBits(d);
    for (int i = 0; i < 8; i++) {
        b[i] = new Long(l).byteValue();
        l = l >> 8;//from w  w w .ja  v a  2s . c om
    }
    return b;
}

From source file:Main.java

public static byte[] toByteArray(double d) {
    long l = Double.doubleToRawLongBits(d);
    return new byte[] { (byte) ((l >> 56) & 0xff), (byte) ((l >> 48) & 0xff), (byte) ((l >> 40) & 0xff),
            (byte) ((l >> 32) & 0xff), (byte) ((l >> 24) & 0xff), (byte) ((l >> 16) & 0xff),
            (byte) ((l >> 8) & 0xff), (byte) (l & 0xff), };
}

From source file:Main.java

public static void writeDouble(OutputStream out, double d) throws IOException {
    long l = Double.doubleToRawLongBits(d);
    out.write(new byte[] { (byte) ((l >> 56) & 0xff), (byte) ((l >> 48) & 0xff), (byte) ((l >> 40) & 0xff),
            (byte) ((l >> 32) & 0xff), (byte) ((l >> 24) & 0xff), (byte) ((l >> 16) & 0xff),
            (byte) ((l >> 8) & 0xff), (byte) (l & 0xff) });
}

From source file:Main.java

public static SharedPreferences.Editor putDouble(final SharedPreferences.Editor edit, final String key,
        final double value) {
    return edit.putLong(key, Double.doubleToRawLongBits(value));
}

From source file:Main.java

/**
 * Returns next bigger double value considering precision of the argument.
 * /*w  w w  .j  a  v  a2  s. c  o m*/
 */
public static double nextUp(double d) {
    if (Double.isNaN(d) || d == Double.POSITIVE_INFINITY) {
        return d;
    } else {
        d += 0.0;
        return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d >= 0.0) ? +1 : -1));
    }
}

From source file:Main.java

/**
 * Replacement for the Math.nextUp(...) method that is only available in
 * HONEYCOMB and higher. Dat's some seeeeek sheeet.
 *
 * @param d//from  w w  w.ja va2s  .  com
 * @return
 */
public static double nextUp(double d) {
    if (d == Double.POSITIVE_INFINITY)
        return d;
    else {
        d += 0.0d;
        return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d >= 0.0d) ? +1L : -1L));
    }
}

From source file:Main.java

/**
 * Returns next smaller float value considering precision of the argument.
 * //from w w w. j av  a  2 s.com
 */
public static double nextDown(double d) {
    if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY) {
        return d;
    } else {
        if (d == 0.0f) {
            return -Float.MIN_VALUE;
        } else {
            return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d > 0.0f) ? -1 : +1));
        }
    }
}

From source file:Main.java

private static int getDoubleChars(double number, char[] buf, int charPos, int significantDigits) {
    if (number != number) {
        STR_NAN.getChars(0, STR_NAN_LEN, buf, charPos);
        return charPos + STR_NAN_LEN;
    }//from www  .j  a va 2  s  . co m

    //we need to detect -0.0 to be compatible with JDK
    boolean negative = (Double.doubleToRawLongBits(number) & 0x8000000000000000L) != 0;
    if (negative) {
        buf[charPos++] = '-';
        number = -number;
    }

    if (number == Double.POSITIVE_INFINITY) {
        STR_INFINITY.getChars(0, STR_INFINITY_LEN, buf, charPos);
        return charPos + STR_INFINITY_LEN;
    }

    if (number == 0) {
        buf[charPos++] = '0';
        buf[charPos++] = '.';
        buf[charPos++] = '0';
    } else {
        int exponent = 0;

        // calc. the power (base 10) for the given number:
        int pow = (int) Math.floor(Math.log(number) / LN10);

        // use exponential formatting if number too big or too small
        if (pow < -3 || pow > 6) {
            exponent = pow;
            number /= Math.exp(LN10 * exponent);
        } // if

        // Recalc. the pow if exponent removed and d has changed
        pow = (int) Math.floor(Math.log(number) / LN10);

        // Decide how many insignificant zeros there will be in the
        // lead of the number.
        int insignificantDigits = -Math.min(0, pow);

        // Force it to start with at least "0." if necessarry
        pow = Math.max(0, pow);
        double divisor = Math.pow(10, pow);

        // Loop over the significant digits (17 for double, 8 for float)
        for (int i = 0, end = significantDigits + insignificantDigits, div; i < end; i++) {

            // Add the '.' when passing from 10^0 to 10^-1
            if (pow == -1) {
                buf[charPos++] = '.';
            } // if

            // Find the divisor
            div = (int) (number / divisor);
            // This might happen with 1e6: pow = 5 ( instead of 6 )
            if (div == 10) {
                buf[charPos++] = '1';
                buf[charPos++] = '0';
            } // if
            else {
                buf[charPos] = (char) (div + '0');
                charPos++;
            } // else

            number -= div * divisor;
            divisor /= 10.0;
            pow--;

            // Break the loop if we have passed the '.'
            if (number == 0 && divisor < 0.1)
                break;
        } // for

        // Remove trailing zeros
        while (buf[charPos - 1] == '0')
            charPos--;

        // Avoid "4." instead of "4.0"
        if (buf[charPos - 1] == '.')
            charPos++;
        if (exponent != 0) {
            buf[charPos++] = 'E';
            if (exponent < 0) {
                buf[charPos++] = '-';
                exponent = -exponent;
            }
            if (exponent >= 100)
                buf[charPos++] = (char) (exponent / 100 + '0');
            if (exponent >= 10)
                buf[charPos++] = (char) (exponent / 10 % 10 + '0');
            buf[charPos++] = (char) (exponent % 10 + '0');
        } // if
    }
    return charPos;
}

From source file:Main.java

public static void fromDouble(byte[] bytes, double value, int offset) {
    fromLong(bytes, Double.doubleToRawLongBits(value), offset);
}