Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1199 :

Posted: Thu Sep 25, 2014 5:42 pm
by jbilkes
API says:

round
public static long round(double a)
Returns the closest long to the argument, with ties rounding to positive infinity.
Special cases:

If the argument is NaN, the result is 0.
If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
Parameters:
a - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
See Also:
Long.MAX_VALUE, Long.MIN_VALUE


I say:

why is there no reference to what happens when the double value is exactly (as far as thats possible for floating numbers) between 2 integers? this is totally skipped over in the question. I'm used to round to the ceiling instead of the floor, meaning that round(0.5) == 1 but appearently this isnt the case and im quite suprised this isnt discussed in the answers to the question or at least in the API ;-)

Re: About Question enthuware.ocajp.i.v7.2.1199 :

Posted: Thu Sep 25, 2014 11:42 pm
by admin
This question touches upon this aspect and the explanation does mention that Math.round(-0.5) evaluates to 0.0.

You make a good point that it would probably help readers to explain how rounding works.

I can only guess that the API probably doesn't mention it explicitly because rounding is a standard mathematical procedure where the number that lies exactly between two numbers always rounds up to the higher one. So .5 rounds to 1 and -.5 rounds to 0. This is not a special case from Java perspective. Long.MIN_VALUE ad MAX_VALUE are special cases in Java that is probably why the API makes it clear as to what happens in their case.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1199 :

Posted: Fri Sep 26, 2014 5:36 pm
by jbilkes
omg ur totally right i kind of oversaw the minus sign... #being ashamed

at least i got 83% on exam 5 today ;-)

Re: About Question enthuware.ocajp.i.v7.2.1199 :

Posted: Thu May 07, 2015 9:21 am
by scranen
rounding is a standard mathematical procedure where the number that lies exactly between two numbers always rounds up to the higher one
For the corner case of numbers that lie exactly between two integers, rounding is not at all a standard mathematical procedure (see, e.g. this explanation or the Java RoundingMode enum).

In C++ for instance, halves are rounded away from zero, making round(-0.5)=-1. This corresponds to the RoundingMode.UP strategy. Rounding of doubles in Java uses a strategy that is not available in the RoundingMode enum, but which should probably be called HALF_FLOOR according to their naming scheme.

Re: About Question enthuware.ocajp.i.v7.2.1199 :

Posted: Thu May 07, 2015 10:39 am
by admin
Thanks for the information, scranen.
-Paul.