• 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

RHE Final #27

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In RHE Final Exam Q#27 asks:
What is the value of the following expression?
Math.round(Math.random() + 2.50001);
Possible answers are
a) 2
b) 3
c) It is impossible to say.
The correct answer in the book is b) and the following explanation is provided:
" Math.random() returns a double greater than or equal to 0.0 and less than 1.0. Math.random() + 2.50001 is a double greater than 2.5 and less than 3.5. Math.round() of any number between but not including 2.5 and 3.5 is 3."
However, what happens if Math.random() returns 0.999999? If I am not mistaken, 0.999999 + 2.50001 = 3.500009 which should round up to 4, not 3. Am I missing something?
Thanks,
Milena
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milena,
I ran some code to test your theory

Guess what? Result = 4!
When your not sure of something, best teacher is the compiler
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Milena Khlabystova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jane.
That's what I thought... and ran the exact same code . I thought that may be I was missing something in Math.random() method and/or precision mechanism, but didn't really want to run a program for days in hope that Math.random() at some point returns 0.999999 (hmm... what's the probability of that?) and proves my point.
Thanks again,
Milena
 
Milena Khlabystova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um... I obviously have to do more programming...
What I thought would be a day turned out to be a second.
Now I feel embarassed...
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milena,
Don't be ... I ran the code as I did for the same reason; didn't want to wait for random to return .999999
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milena,
The problem has already been fixed in the errata. Make sure you check it before you go any further.
Click Here!
Regards,
Manfred.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic