• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Math.round

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Question regarding Math.round. As per K&B book page 371. It says add 0.5 to the number and if decimal is greater than 0.5 round() is equal to Math.ceil, if decimal is less than 0.5 round is equal to Math.floor

I did the following


but if i apply the algorithm above, it will be
Math.ceil(5.5) --> 6.0
Math.ceil(-2.5) --> -2.0
Math.ceil(3.75) --> 4.0
Math.ceil(-4.7) --> -4.0

Is my understanding is correct ?

Please help
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Lincoln:
Hi,

Question regarding Math.round. As per K&B book page 371. It says add 0.5 to the number and if decimal is greater than 0.5 round() is equal to Math.ceil, if decimal is less than 0.5 round is equal to Math.floor

I did the following


but if i apply the algorithm above, it will be
Math.ceil(5.5) --> 6.0
Math.ceil(-2.5) --> -2.0
Math.ceil(3.75) --> 4.0
Math.ceil(-4.7) --> -4.0

Is my understanding is correct ?

Please help



I don't have the book in front of me, but I think you may be combining statements.

If you look at the second part of what you said, if the decimal part is greater than 0.5, the result of round is the same as the result of ceil, otherwise the result of round is the same as the result of floor.

Look, for example, at 3.2.

The decimal part of the number is 0.2 which is less than 0.5. So the result of Math.round(3.2) is equal to the value of Math.floor(3.2).

If you look at 3.7,

The decimal part of the number is 0.7 which is greater than 0.5. So the result of Math.round(3.7) is equal to the value of Math.ceil(3.7).

Basically rounding is asking which integer is the closest to the number.

The part about adding 0.5 to the number is the way that Math.round works.

Math.round is equal to Math.floor of the number plus 0.5 (the result is cast to the appropriate primitive).
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Lincoln:
...As per K&B book page 371. It says add 0.5 to the number and if decimal is greater than 0.5 round() is equal to Math.ceil, if decimal is less than 0.5 round is equal to Math.floor...




Is that an exact quote? It addresses greater than and less than, but doesn't seem to address equal to. Even putting that aside, I still don't understand the quote. As Keith mentioned above, the API describes Math.round(double a) as equal to (long)Math.floor(a + 0.5d).
[ March 07, 2007: Message edited by: marc weber ]
 
John Lincoln
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. Pardon me i still don't get it. For round() i think if decimal is > 0.5 it should next whole integer, does not matter +ve or
-ve


I don't cleary understand Math.round is equal to Math.floor + 0.5. Can you please explain

Thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Lincoln:
...I don't cleary understand Math.round is equal to Math.floor + 0.5. Can you please explain...


First, we add 0.5 to the value, then take the floor of the result. As the API says, Math.floor(double a)...

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.


First, note that we are looking for a value "less than or equal to the argument." So if you imagine the argument on the number line, our range of possible results includes the argument and everything to the left. Now, within this range, we find the integer that is "closest to positive infinity" -- or in other words, the right-most integer.

So for example, if we have -3.2, we first add 0.5 to get -2.7. Now, our range of possible results is everything to the left of (and including) -2.7. Next we look for the right-most integer within that range, which is -3.0. Casting to type long, this is -3.

(Note that round is overridden to take a float or a double. The float implementation returns an int, and the double implementation returns a long.)
[ March 07, 2007: Message edited by: marc weber ]
 
This is my favorite tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic