*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes floating literal question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "floating literal question" Watch "floating literal question" New topic
Author

floating literal question

Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
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?


SCJP 1.5<br />SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
I know that JLS section but the questions are WHY and HOW ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

Joined: Aug 03, 2002
Posts: 7729
After working it out for myself, by writing a program, I found this (at the end of the article)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: floating literal question
 
Similar Threads
levirD sselgninaeM
casting of objects
getInitParameter
Question on exercise about Exception
one mock question for exception