• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Mantissa and exponent from double?

 
Sundar Ram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks in advance.
 
Sundar Ram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks in advance.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Thanks in advance.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48382
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic