Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Get Mantissa and exponent from double?

Ranch Hand
Posts: 102
Hi,

How do I got Mantissa and exponent from double?

For eg.
* If the value is 15.0E0 then I want to get mantissa =.25000, exponent = 2
* If the value is 1.3E-3 then I want to get mantissa = .13000, exponent = -2

How can we do this in java? Any pointers is highly appreciated.

Sundar Ram
Ranch Hand
Posts: 102
There are some typo, here is the correct one:

Hi,

How do I get Mantissa and exponent from double?

For eg.
* If the value is 15.0E0 then I want to get mantissa =.15000, exponent = 2
* If the value is 1.3E-3 then I want to get mantissa = .13000, exponent = -2

How can we do this in java? Any pointers is highly appreciated.

Marshal
Posts: 58378
178
I think you would have to use Math#log to get the exponent, then divide. Remember that double numbers are stored as binary mantissa and exponent and 0=positive, 1=negative.

author and iconoclast
Sheriff
Posts: 24217
38
Yes. You could also look at the Double.doubleToLongBits() method, which gives you the exact bit representation of the double. You can then use bit masks and shift operators to extract the values you're after.

Sundar Ram
Ranch Hand
Posts: 102
• 1

But it does not return 0.15 as mantissa and 2 as exponent.

Campbell Ritchie
Marshal
Posts: 58378
178

Originally posted by Ernest Friedman-Hill:
Yes. You could also look at the Double.doubleToLongBits() method, which gives you the exact bit representation of the double. You can then use bit masks and shift operators to extract the values you're after.

But that would give mantissa and exponent in binary, and it would also miss out the extra 1. which is added to the mantissa.

Campbell Ritchie
Marshal
Posts: 58378
178
• 1
You will have to look up the JLS specification for floating-point numbers, and also the IEEE754 standard. There is a description in hardware books, for example I have Alan Clements: the Principles of Computer Hardware (3/e), Oxford: Oxford University Press (2000), page 187-189.

For normalised double numbers, you get
• Sign bit: 1 bit.
• Binary exponent, biased by 1023: 11 bits.
• Mantissa: 53 bits.
• Total: 65 bits. That is done by normalising the mantissa so it always lies in the range 1.00000000000000etc...1.111111111111etc and discarding the 1. So you get 53 bits into 52 bits worth of space and can still fit it into 64 bits. So your mask with 0x000ffffffffffffL will have to becomeThe () are not strictly necessary. You would then have to convert it back to a double using OR with (I think) 0x3ff0000000000000L.

Maybe logs are easier.
[ June 19, 2008: Message edited by: Campbell Ritchie ]

Sundar Ram
Ranch Hand
Posts: 102

But that would give mantissa and exponent in binary, and it would also miss out the extra 1. which is added to the mantissa.

How can we convert the binary mantissa and exponent back?
Do you mean there will be a loss of precision?

Thanks again.

Sundar

Campbell Ritchie
Marshal
Posts: 58378
178
There is a method in Double which converts long bits to double.

And you already know there is no such thing as precision with floating-point I don't think you will lose any precision like this, but you might lose sleep over working out the correct masks.

How about the %a tag in printf?

 It is sorta covered in the JavaRanch Style Guide.