• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Confusion Math.random()

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 371, of K&B book, it says that
"If the number after the decimal point is greater than
or equal to 0.5, Math.round() is equal to Math.ceil()."
Then why is this not true?
Math.random(-10.8) == Math.ceil (-10.8)
Math.random(-10.8) --> -11
Math.ceil (-10.8) --> -10
Thanks,
Cathy.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:

Math.random(-10.8) --> -11
Math.ceil (-10.8) --> -10
Thanks,
Cathy.


Hi Cathy,
First of all, I think you meant
Math.round(-10.8) --> -11
insead of
Math.random(-10.8) --> -11

Now for the definition.
This is how ceil() is defined in the API,


Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer


and this is how round() is defined:


Returns the closest long to the argument


Take note of the phrase "that is not less than" for ceil(). If you apply that in your example, the number(a double value) that is not less than -10.8 is -10. If you are thinking -11 that number is less than -10.8
Now, consider the phrase "closest long" for round(). And in your example, the closest long value to -10.8 is -11.
The trick that I use to remember how ceil() works is this.
Ceil is short for ceiling. So what I do is imagine a vertical number line like this:

Now if you want to get the ceil() of a FP number, say 2.5, all you need to do is look up as in look up the ceiling. So looking up the number line you will get the answer 3.0. And if you are given a negative number, say -4.5, you again look up to find the answer -4.0.
You can also use the same trick for floor() but instead of looking up you look down, as in look down the floor.
Hope this helps.
[ September 27, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Cathy,
I had come across an equation in this forum which helps to solve all such questions.
Math.round(arg) =>
Math.floor(arg + 0.5)
The equation is also applicable in case of -ve numbers.
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to cast the result into int type. But I think u understood what the result is.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
So is it safe to say that
For positive numbers :"If the number after the decimal point is greater than or equal to 0.5, Math.round() is equal to Math.ceil()."
For negative numbers: "If the number after the decimal point is greater than 0.5, Math.round() is equal to Math.floor()."
Round 10.2: 10(floor)
Round 10.5: 11(ceil)
Round 10.8: 11(ceil)
Round -10.2: -10 (ceil)
Round -10.5: -10 (ceil)
Round -10.8: -11 (floor)
Thanks,
Cathy.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic