Win a copy of Rust Web Development this week in the Other Languages forum!
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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

Math.round() question

Greenhorn
Posts: 20
• • Number of slices to send:
Optional 'thank-you' note:
• • 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?

Jonathan

Sheriff Posts: 7023
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • sudhir
does
round(-5.5) gives -5

Sudhir Meduri
Greenhorn
Posts: 22
• • Number of slices to send:
Optional 'thank-you' note:
• • 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: 13019
66   • • Number of slices to send:
Optional 'thank-you' note:
• • 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. 