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.

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.

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.

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 it does not return 0.15 as mantissa and 2 as exponent.

Thanks in advance.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42563

31

posted

0

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
Sheriff

Joined: Oct 13, 2005
Posts: 42563

31

posted

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

Joined: May 22, 2006
Posts: 102

posted

0

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
Sheriff

Joined: Oct 13, 2005
Posts: 42563

31

posted

0

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.