Example usage for java.lang Double longBitsToDouble

List of usage examples for java.lang Double longBitsToDouble

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static native double longBitsToDouble(long bits);

Source Link

Document

Returns the double value corresponding to a given bit representation.

Usage

From source file:Main.java

/**
 * Returns next smaller float value considering precision of the argument.
 * //  www  .jav a  2 s . c  o m
 */
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

public static double readDouble(InputStream in) throws IOException {
    long bits = ((long) (in.read() & 0xff) << 56) | ((long) (in.read() & 0xff) << 48)
            | ((long) (in.read() & 0xff) << 40) | ((long) (in.read() & 0xff) << 32) | ((in.read() & 0xff) << 24)
            | ((in.read() & 0xff) << 16) | ((in.read() & 0xff) << 8) | (in.read() & 0xff);
    return Double.longBitsToDouble(bits);
}

From source file:Main.java

public static double getDouble(byte[] b, int index) {
    long l = b[0];
    l &= 255L;//from  w ww  . j a  v a  2 s .  c o  m
    l |= b[1] << 8;
    l &= 65535L;
    l |= b[2] << 16;
    l &= 16777215L;
    l |= b[3] << 24;
    l &= 4294967295L;
    l |= b[4] << 32;
    l &= 1099511627775L;
    l |= b[5] << 40;
    l &= 281474976710655L;
    l |= b[6] << 48;
    l &= 72057594037927935L;
    l |= b[7] << 56;
    return Double.longBitsToDouble(l);
}

From source file:Main.java

public static double byteToDouble(byte[] b) {
    if (b.length != 8) {
        return -1;
    }//from ww  w .  ja v a2  s  .  co m
    long l;
    l = b[0];
    l &= 0xff;
    l |= ((long) b[1] << 8);
    l &= 0xffff;
    l |= ((long) b[2] << 16);
    l &= 0xffffff;
    l |= ((long) b[3] << 24);
    l &= 0xffffffffl;
    l |= ((long) b[4] << 32);
    l &= 0xffffffffffl;

    l |= ((long) b[5] << 40);
    l &= 0xffffffffffffl;
    l |= ((long) b[6] << 48);
    l &= 0xffffffffffffffl;

    l |= ((long) b[7] << 56);

    return Double.longBitsToDouble(l);
}

From source file:Main.java

public static double getDouble(SharedPreferences pref, String key, double defVal) {
    return Double.longBitsToDouble(pref.getLong(key, Double.doubleToLongBits(defVal)));
}

From source file:Main.java

public static double toDouble(byte[] bytes, int offset) {
    return Double.longBitsToDouble(toLong(bytes, offset));
}

From source file:Main.java

public static double longBitsToDouble(long value) {
    return Double.longBitsToDouble(value);
}

From source file:Main.java

/**
 * Converts a byte[8] binary double value into a double primitive.
 *
 * @param bytes a byte[8] to be converted.
 * @return a double value.//ww w  .java  2s .c  o m
 */
public static final double registersToDouble(byte[] bytes) {
    return Double.longBitsToDouble(((((long) (bytes[0] & 0xff) << 56) | ((long) (bytes[1] & 0xff) << 48)
            | ((long) (bytes[2] & 0xff) << 40) | ((long) (bytes[3] & 0xff) << 32)
            | ((long) (bytes[4] & 0xff) << 24) | ((long) (bytes[5] & 0xff) << 16)
            | ((long) (bytes[6] & 0xff) << 8) | ((long) (bytes[7] & 0xff)))));
}

From source file:Main.java

/**
 * Converts a "double" value between endian systems.
 * @param value value to convert//from  w  w w .ja  v a  2  s.com
 * @return the converted value
 */
public static double swapDouble(double value) {
    return Double.longBitsToDouble(swapLong(Double.doubleToLongBits(value)));
}

From source file:Main.java

public static boolean isLikelyDouble(long value) {
    // Check for some common named double values
    // We don't check for Double.MIN_VALUE, which has a long representation of 1
    if (value == canonicalDoubleNaN || value == maxDouble || value == piDouble || value == eDouble) {
        return true;
    }/*from   w  ww . j  a va 2s .c om*/

    // Check for some named long values
    if (value == Long.MAX_VALUE || value == Long.MIN_VALUE) {
        return false;
    }

    // a non-canocical NaN is more likely to be an long
    double doubleValue = Double.longBitsToDouble(value);
    if (Double.isNaN(doubleValue)) {
        return false;
    }

    // Otherwise, whichever has a shorter scientific notation representation is more likely.
    // Long wins the tie
    String asLong = format.format(value);
    String asDouble = format.format(doubleValue);

    // try to strip off any small imprecision near the end of the mantissa
    int decimalPoint = asDouble.indexOf('.');
    int exponent = asDouble.indexOf("E");
    int zeros = asDouble.indexOf("000");
    if (zeros > decimalPoint && zeros < exponent) {
        asDouble = asDouble.substring(0, zeros) + asDouble.substring(exponent);
    } else {
        int nines = asDouble.indexOf("999");
        if (nines > decimalPoint && nines < exponent) {
            asDouble = asDouble.substring(0, nines) + asDouble.substring(exponent);
        }
    }

    return asDouble.length() < asLong.length();
}