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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post 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 ;-)

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post by jbilkes »

omg ur totally right i kind of oversaw the minus sign... #being ashamed

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

scranen
Posts: 12
Joined: Thu May 07, 2015 8:55 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Thanks for the information, scranen.
-Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest