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 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!

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 ]

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)

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

Yeah, I got confused too after reading that particular bit. At the end I just use Math.floor(x + 0.5).

SCJP 1.4

Jonathan Oblea
Greenhorn

Joined: Jul 29, 2002
Posts: 20

posted

0

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

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Don't get me started about those stupid light bulbs.