Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# floating literal question

Rupak Khurana
Ranch Hand
Posts: 89
Why is double d = 0x1234.1234 a compiler error ?

d = 0x1234.1234p+1 is fine. how are the values computed when the exponent p is used?

Barry Gaunt
Ranch Hand
Posts: 7729
Take a look at JLS 3 section 3.10.2.
The relevant bit is:

The BinaryExponent is not optional.
(note: FloatTypeSuffixopt is one of f F d D)
[ July 29, 2005: Message edited by: Barry Gaunt ]

Rupak Khurana
Ranch Hand
Posts: 89
I know that JLS section but the questions are WHY and HOW ?

Barry Gaunt
Ranch Hand
Posts: 7729
Why is double d = 0x1234.1234 a compiler error ?

Why?: Because it does not conform to the syntax rules.

d = 0x1234.1234p+1 is fine. how are the values computed when the exponent p is used?

How?:
0x1234.1234p+1 = ((((1*16 + 2)*16 + 3)*16 + 4) + (1.0+(2.0+(3.0+4.0/16)/16)/16)/16)*2

p1 is 2
p2 is 2*2
p3 is 2*2*2
pn where n is decimal is 2 to the power n.

And:
p0 is 1
p-1 is 1.0/2
p-2 is 1.0/(2*2), and so on.
[ July 29, 2005: Message edited by: Barry Gaunt ]

Barry Gaunt
Ranch Hand
Posts: 7729
After working it out for myself, by writing a program, I found this (at the end of the article)