• 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

Math.random() & .round() question from RHE

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question from RHE is:
"What is the value of the following expression?
Math.round(Math.random() + 2.50001);"
Answers: A) 2; B) 3; C) It is impossible to say.
The answer is 3 (not "It is impossible to say").
This implies that you cannot get a result of 3.5
The implication of this is that Math.random() won't return .99999. I realise it's result is 0<=x<1.0. So is the result only to four decimal places (ie max value is 0.9999)?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Trevor Green:
A question from RHE is:
"What is the value of the following expression?
Math.round(Math.random() + 2.50001);"
Answers: A) 2; B) 3; C) It is impossible to say.
The answer is 3 (not "It is impossible to say").
This implies that you cannot get a result of 3.5
The implication of this is that Math.random() won't return .99999. I realise it's result is 0<=x<1.0. So is the result only to four decimal places (ie max value is 0.9999)?



Hi,
Math.random() results in double primitive type and you get result to approx. 12 decimal places but it will always be less than 1 so after adding to 2.50001 it will always be very close to 3 but never equal to 3, and Math.round() results in long primitive type so you get 3
I hope its clear
Jyotsna
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Math.random() returns a double to 12 decimal places, then 0.999999999999 is possible.
2.50001 + 0.99999999999 = 3.5000009999
This would round to 4. But the answer is B) 3.
Isn't it c) It is impossible to say, because, in this rarest of cases, it could also be 4, as well as 3?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with you.
-Anand

[This message has been edited by anand raman (edited June 13, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic