Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of The Mikado Method this week in the Agile and other Processes forum!

# Question about floats and doubles

ed suttner
Greenhorn

Joined: Mar 04, 2003
Posts: 13
I am studying for the Sun's Programmer test and have a question. I should know this, but I am having some difficulty. How do you know that 11301874.9881024 is a double and not a float? It is the number of digits after the decimal point. Please be kind. Like I said I should know this but am having some problems.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Ed, welcome to JavaRanch.
A double literal is like 123.4567, 123.4567D, or 123.4567d.
A float literal is like 123.4567F or 123.4567f.
The important thing to realize is 123.4567 without a D, d, f, or F is a double.
[ March 04, 2003: Message edited by: Barry Gaunt ]

Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Or do you mean:
Is 11301874.9881024 a valid double literal?
or
Is 11301874.9881024F a valid float literal?
In that case: Pass (for now )
ed suttner
Greenhorn

Joined: Mar 04, 2003
Posts: 13
Thanks for the welcome. I think I get it now. Here is the question that I was having problems with (p. 48, #9 if you have the JAVA 2 book by Sierra and Bates).
Which of these are valid delarations of a float? (Choose three.)
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;
I now know why B is not an answer. It would be right if there was a F behind the number.
The book says that B, D, and E are all doubles.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

I now know why B is not an answer. It would be right if there was a F behind the number.

Exactly!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Barry Gaunt:
Or do you mean:
Is 11301874.9881024 a valid double literal?
or
Is 11301874.9881024F a valid float literal?
In that case: Pass (for now )

Regarding this, I guess the constants Double.MIN_VALUE, Double.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE could be used to find out the validity of such values. Otherwise the IEEE floating point standatds would be the place to look.
Cheers,
-Barry

I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week

subject: Question about floats and doubles
Where to ask Data Layer Questions
question on security constraints