aspose file tools*
The moose likes Beginning Java and the fly likes Get Mantissa and exponent from double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get Mantissa and exponent from double?" Watch "Get Mantissa and exponent from double?" New topic
Author

Get Mantissa and exponent from double?

Sundar Ram
Ranch Hand

Joined: May 22, 2006
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.

Thanks in advance.


SCJP 5.0 | SCWCD 1.4
http://heysundar.blogspot.com
Sundar Ram
Ranch Hand

Joined: May 22, 2006
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.

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Sundar Ram
Ranch Hand

Joined: May 22, 2006
Posts: 102


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

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
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: 38788
    
  23
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
    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: 38788
        
      23
    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?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Get Mantissa and exponent from double?