• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double.toHexString.

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (m x 2^e), where m is the significand (a.k.a. mantissa) and e is the exponent.


  • In base 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))
  • 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 ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic