简体   繁体   中英

How do you get the mantissa of a float in java?

I'm trying to get the mantissa of a float (just to learn), but it isn't working as expected.

浮点数的结构

The mantissa of say 5.3 is 53, right? I tried this code:

System.out.println(Float.floatToIntBits(5.3f) & 0x7FFFFF);

It printed 2726298 . Shouldn't it remove the exponent bits and leave 53? I tried plenty of things, but this always happens. What am I doing wrong here?

The formula for single precision following the IEEE standard is:

(-1)^sign + 1.Mantissa x 2^(Exponent - Bias)

So 5.3 base 10 is 101.0100110011001100110011 base 2

101.0100110011001100110011 = 1.010100110011001100110011 * 2^2

2^2 = 2^(exp - bias) having bias = 127 (according to the IEEE standard for single precision)
so: exp - 127 = 2 => exp = 129 base 10 or 10000001 base 2

Single precision table:

0 | 10000001 | 01010011001100110011001

Sign = 0
Exp = 129
Mantissa = 2726297

From the article IBM: Java's new math. Floating-point numbers ( in Russian ) the simplest way to get the mantissa is:

public static double getMantissa(double x) {
    int exponent = Math.getExponent(x);
    return x / Math.pow(2, exponent);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM