Get Mantissa and exponent from double?
Sundar Ram
Ranch Hand
Posts: 102
posted 8 years ago
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.3E3 then I want to get mantissa = .13000, exponent = 2
How can we do this in java? Any pointers is highly appreciated.
Thanks in advance.
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.3E3 then I want to get mantissa = .13000, exponent = 2
How can we do this in java? Any pointers is highly appreciated.
Thanks in advance.
SCJP 5.0  SCWCD 1.4
http://heysundar.blogspot.com
Sundar Ram
Ranch Hand
Posts: 102
posted 8 years ago
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.3E3 then I want to get mantissa = .13000, exponent = 2
How can we do this in java? Any pointers is highly appreciated.
Thanks in advance.
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.3E3 then I want to get mantissa = .13000, exponent = 2
How can we do this in java? Any pointers is highly appreciated.
Thanks in advance.
SCJP 5.0  SCWCD 1.4
http://heysundar.blogspot.com
Campbell Ritchie
Marshal
Posts: 53147
124
Sundar Ram
Ranch Hand
Posts: 102
posted 8 years ago
But it does not return 0.15 as mantissa and 2 as exponent.
Thanks in advance.
 1
But it does not return 0.15 as mantissa and 2 as exponent.
Thanks in advance.
SCJP 5.0  SCWCD 1.4
http://heysundar.blogspot.com
Campbell Ritchie
Marshal
Posts: 53147
124
posted 8 years ago
But that would give mantissa and exponent in binary, and it would also miss out the extra 1. which is added to the mantissa.Originally posted by Ernest FriedmanHill:
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.
Campbell Ritchie
Marshal
Posts: 53147
124
posted 8 years ago
 1
You will have to look up the JLS specification for floatingpoint 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 187189.
For normalised double numbers, you getSign 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 ]
For normalised double numbers, you get
Maybe logs are easier.
[ June 19, 2008: Message edited by: Campbell Ritchie ]
Sundar Ram
Ranch Hand
Posts: 102
posted 8 years ago
How can we convert the binary mantissa and exponent back?
Do you mean there will be a loss of precision?
Thanks again.
Sundar
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
SCJP 5.0  SCWCD 1.4
http://heysundar.blogspot.com
Campbell Ritchie
Marshal
Posts: 53147
124
posted 8 years ago
There is a method in Double which converts long bits to double.
And you already know there is no such thing as precision with floatingpoint 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?
And you already know there is no such thing as precision with floatingpoint 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?
If I had asked people what they wanted, they would have said faster horses  Ford. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
