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

Math.round() question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I have read the round() method from K&B book and i'm a little confused
on the statement below:
"If the number after the decimal point is less than 0.5 then
Math.round() is equal to Math.floor(). If the number after the
decimal point is greater than equal 0.5 then Math.round() is
equal to Math.ceil()".
Maybe I misunderstood the statement above. Can someone please give me
guidance on this topic?
Example:
1.) Math.round(-5.4) returns -5
Math.floor(-5.4) returns -6
Note: The number after the decimal is less than 0.5 which means
Math.round() should be equal to Math.floor() but it the
result doesn't match?

2.) Math.round(-5.6) returns -6
Math.ceil (-5.6) returns -5
Note: The number after the decimal is greater than equal to 0.5
which means Math.round() should be equal to Math.ceil() but
the result doesn't match?

Thank you in advance,
Jonathan
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jonathan O.,
Welcome back, to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. We'd like it if your last name contained more than just one letter.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like Kathy and Bert weren't thinking about negative numbers for that explanation of how Math.round() works.
I prefer how the Math class documentation describes the behavior of Math.round().

Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression:
(int)Math.floor(a + 0.5f)

If it's not already reported, and it doesn't look as if it has been, you might do us all a favor and email Kathy and/or Bert about this error.
[ January 07, 2004: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
Which means that the statement from the book you have cited is correct for positive numbers only (and it's not correct for negatives)
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jonathan,
Kathy and Bates did not include negative floats or doubles for explanation of the Math.round() method I had to try and figure it out, perhaps they thought it was too obvious? but they did point out the all interesting -0.5 behaviour:
int i=Math.round(-10.5f); //gives -10
int j=Math.round(10.5f); //gives 11
both are ceil()'ed.
This is my first post so coudnt resist pointing out the 0.5 behavior
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I got confused too after reading that particular bit. At the end I just use Math.floor(x + 0.5).
 
Jonathan Oblea
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All, Thank you for your replies.
Can I assume for Math.round() the following?
For positive numbers the following can be applied:
1. less than 0.5 = floor()
2. greater than equal to 0.5 = ceil()
For negative numbers the following can be applied:
1. greater than 0.5 = floor()
2. less than equal to 0.5 = ceil()
Thanks again,
Jonathan
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think after looking the following result
Math.round(5.4) :5
Math.round(5.5) :6
Math.round(5.9) :6
Math.round(-5.4) :-5
Math.round(-5.5) :-5
Math.round(-5.9) :-6
I will simply follow the thumb rule
Add +0.5 (no matter pos or neg ) and floor it.
Pls correct me if I am wrong.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sudhir
does
round(-5.5) gives -5
 
Sudhir Meduri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it did gave to me
System.out.println ("Math.round(-5.5) :"+Math.round(-5.5)); = -5
i'm using jdk 1.4.2 version (in case..)
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For negative numbers the following can be applied:
1. greater than 0.5 = floor()
2. less than equal to 0.5 = ceil()


This makes the former math teacher in me cringe. because, since we're talking about negative numbers, "greater than 0.5" is confusing. -0.4 is greater than -0.5.
I think what you mean is "larger magnitude" than -0.5
This is why it's probably always better to use the "add 0.5 and take the floor" rule. it's less confusing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic