• 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

Question about floats and doubles

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
Answer: A, C, F
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic