Example usage for java.lang Math getExponent

List of usage examples for java.lang Math getExponent

Introduction

In this page you can find the example usage for java.lang Math getExponent.

Prototype

public static int getExponent(double d) 

Source Link

Document

Returns the unbiased exponent used in the representation of a double .

Usage

From source file:Main.java

public static void main(String[] args) {
    int exp = Math.getExponent(17.0);
    System.out.println("Math.getExponent (17.0) = " + exp);

}

From source file:Main.java

public static void main(String[] args) {

    System.out.println(Double.MAX_EXPONENT);
    System.out.println(Math.getExponent(Double.MAX_VALUE));
}

From source file:FloastPointDemo.java

public static void main(String[] args) {
    // Returns the unbiased exponent value of a double,
    // where 2^exp <= d. In this case, 2^4 <= 17
    int exp = Math.getExponent(17.0);
    System.out.println("Math.getExponent (17.0) = " + exp);

}

From source file:Main.java

public static void main(String[] args) {

    float x = 12345.6f;
    float y = -123.45f;

    // print the unbiased exponent of them
    System.out.println("Math.getExponent(" + x + ")=" + Math.getExponent(x));
    System.out.println("Math.getExponent(" + y + ")=" + Math.getExponent(y));
    System.out.println("Math.getExponent(0f)=" + Math.getExponent(0f));

}

From source file:Main.java

public static void main(String[] args) {

    double x = 123456.7;
    double y = -123.45;

    // print the unbiased exponent of them
    System.out.println("Math.getExponent(" + x + ")=" + Math.getExponent(x));
    System.out.println("Math.getExponent(" + y + ")=" + Math.getExponent(y));
    System.out.println("Math.getExponent(0)=" + Math.getExponent(0));

}

From source file:Main.java

/**
 * <p>// w w w.  j  a v a  2s .co m
 * Calculates the bucket id the key should be placed in according to the
 * first n bits of the key where n is the power of 2 numBuckets is defined
 * as. F.e if 1024 buckets are defined the first 10 bits of the key will be
 * taken as the index for the actual bucket (2^10 = 1024, 2^9 = 512, ...)
 * </p>
 * 
 * @param key
 *            The 64bit key whose bucket index should be calculated
 * @param numBuckets
 *            The total number of available buckets. This should be a power
 *            of two (e.g. 2, 4, 8, 16, 32, ...)
 * @return The bucket index the key should be in
 */
public static int getBucketForKey(long key, final int numBuckets) {
    // test if numBuckets is a power of 2
    int exponent = Math.getExponent(numBuckets);
    if (numBuckets != Math.pow(2, exponent))
        throw new IllegalArgumentException("Number of buckets does not correspond to a power of 2!");
    return (int) (key >> (64 - exponent)) + numBuckets / 2;
}

From source file:Main.java

/**
 * Fills the array with random floats.  Values will be between min (inclusive) and
 * max (inclusive).//from   w  ww. ja  v a2  s .  c o m
 */
public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            float mantissa = r.nextFloat();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            float rand = sign * mantissa * (float) Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            float rand = r.nextFloat();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        float f = (float) sInterestingDoubles[i];
        if (min <= f && f <= max) {
            array[r.nextInt(array.length)] = f;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Float.NaN;
        array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.MIN_VALUE;
        array[r.nextInt(array.length)] = Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = Float.MAX_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Float.MAX_VALUE;
    }
}

From source file:Main.java

/**
 * Fills the array with random doubles.  Values will be between min (inclusive) and
 * max (inclusive)./*w  ww .j  av  a  2 s.  c  om*/
 */
public static void genRandomDoubles(long seed, double min, double max, double array[],
        boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            double mantissa = r.nextDouble();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            double rand = sign * mantissa * Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            double rand = r.nextDouble();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        double d = sInterestingDoubles[i];
        if (min <= d && d <= max) {
            array[r.nextInt(array.length)] = d;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Double.NaN;
        array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.MIN_VALUE;
        array[r.nextInt(array.length)] = Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = Double.MAX_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Double.MAX_VALUE;
    }
}

From source file:com.idylwood.utils.MathUtils.java

static final void compare(final String msg, final double d1, final double d2) {
    final double diff = d1 - d2;
    System.out.println("Diff " + msg + ":" + diff);
    System.out.println("Exponent:" + Math.getExponent(diff));
    System.out.println("Mantissa:" + mantissa(diff));
    System.out.println("Precision:" + precision(diff));
}

From source file:com.idylwood.utils.MathUtils.java

static final double mantissa(final double d) {
    return abs(Math.scalb(d, -Math.getExponent(d)));
}