# Double.toHexString.

Deepak Jain

Ranch Hand

Posts: 637

posted 8 years ago

Since K&B book talks about toHexSTring() i think toHexString is for exam,

But it talks only wrt Integer and Long which are fairly simple to understand. But when i ran the below code

Result:

1.0 : 0x1.0p0

2.0 : 0x1.0p1

3.0 : 0x1.8p1

4.0 : 0x1.0p2

5.0 : 0x1.4p2

6.0 : 0x1.8p2

7.0 : 0x1.cp2

8.0 : 0x1.0p3

9.0 : 0x1.2p3

Can someone explain what does 'p' stand for and how did this convertion happen?

I did read Java DOC for Double but there is no clear explanation about the conversion.

Thanks

Deepak

But it talks only wrt Integer and Long which are fairly simple to understand. But when i ran the below code

Result:

1.0 : 0x1.0p0

2.0 : 0x1.0p1

3.0 : 0x1.8p1

4.0 : 0x1.0p2

5.0 : 0x1.4p2

6.0 : 0x1.8p2

7.0 : 0x1.cp2

8.0 : 0x1.0p3

9.0 : 0x1.2p3

Can someone explain what does 'p' stand for and how did this convertion happen?

I did read Java DOC for Double but there is no clear explanation about the conversion.

Thanks

Deepak

SCJP, SCWCD, SCBCD

Kelvin Chenhao Lim

Ranch Hand

Posts: 513

posted 8 years ago

Hi Deepak,

You definitely don't need to know this for the SCJP. Not that that'll stop me from answering your question anyway, though getting the notation right will be hard without superscripts and subscripts.

Firstly, it'll help if you have a general understanding of two things: the IEEE 754 representation for floating-point numbers, and general n-ary fractions. Here are the most essential facts:

You can think of a positive floating-point number in IEEE 754 form as a value of the form (

In base Java represents all floating-point values (i.e. float and double) in IEEE 754 form. Therefore, Double.toHexString()'s output will tell you the significand and exponent. "0x1.8p1" means a significand of 1.8 (in hexadecimal fraction form) and an exponent of 1 (hexadecimal integer). 1.8 in hexadecimal is (1 x 16^0) + (8 x 16^(-1)) == 1.5 in decimal. Therefore, 1.8p1 == 1.5 x 2^1 == 2.

Similarly, "0x1.cp2" == ( (1 x 16^0) + (12 x 16^(-1)) ) x 2^2 == 1.75 x 4 == 7

To make this clearer, here are the IEEE 754 signifcand-exponent representations of integers 1 to 10 expressed in binary, decimal, and hexadecimal form (note that only the significands are expressed as non-decimals):

Hopefully this will also give you some idea of how non-integers are represented in IEEE 754.

[ November 22, 2007: Message edited by: Kelvin Lim ]

You definitely don't need to know this for the SCJP. Not that that'll stop me from answering your question anyway, though getting the notation right will be hard without superscripts and subscripts.

Firstly, it'll help if you have a general understanding of two things: the IEEE 754 representation for floating-point numbers, and general n-ary fractions. Here are the most essential facts:

*m*x 2^

*e*), where

*m*is the significand (a.k.a. mantissa) and

*e*is the exponent.

*n*, the number ab.cdef means (a x n^1) + (b x n^0) + (c x n^(-1)) + (d x n^(-2)) + (e x n^(-3))

Similarly, "0x1.cp2" == ( (1 x 16^0) + (12 x 16^(-1)) ) x 2^2 == 1.75 x 4 == 7

To make this clearer, here are the IEEE 754 signifcand-exponent representations of integers 1 to 10 expressed in binary, decimal, and hexadecimal form (note that only the significands are expressed as non-decimals):

Hopefully this will also give you some idea of how non-integers are represented in IEEE 754.

[ November 22, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0

It is sorta covered in the JavaRanch Style Guide. |