Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

structure of a floating point number

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?

like image 628
user3452725 Avatar asked Jun 17 '14 21:06

user3452725


1 Answers

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);
}
like image 190
Sergey Ponomarev Avatar answered Sep 24 '22 15:09

Sergey Ponomarev