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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Math.round()

Ranch Hand
Posts: 502
• Number of slices to send:
Optional 'thank-you' note:
public class Example {
public static void main(String [] args) {
double values[] = {-2.3, -1.0, 0.25, 4};
int cnt = 0;
for (int x=0; x < values.length; x++) {
if (Math.round(values[x] + .5) == Math.ceil(values[x])) {
++cnt;
}
}
System.out.println("same results " + cnt + " time(s)");
}
}

what is the result?
A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
E. An exception is thrown at runtime.

Explanation Given In K&B Book:

Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it�s as if we are adding 1 then doing a floor(). The values that start out as integer values will in effect be incremented by 1 on the round() side but not on the ceil() side, and the noninteger values will end up equal.

i didn't understand.can anybody explain me please?

Ranch Hand
Posts: 1277
• Number of slices to send:
Optional 'thank-you' note:
the most crucial point in understanding is the behaviour of round method when the arguement is negative.

rest of the code can be understood by draeing a table and filling in aprropriate values as per the methods
we call this handrun

Ranch Hand
Posts: 65
• Number of slices to send:
Optional 'thank-you' note:
Niranjan. i didnt understand anything you said.
What do you mean by crucial point.

Ranch Hand
Posts: 163
• Number of slices to send:
Optional 'thank-you' note:
public static int round(float a)
Returns the closest int to the argument. The result is rounded to an integer by adding 0.5, taking the floor of the result, and casting the result to type int.

Eg1: Math.round(35.6)
Step 1: add 0.5 i.e. 35.6 + 0.5 = 36.1
Step 2: find the floor ( i.e. value towards �ve infinity i.e. greatest integer less than
or equal to given value)
for 36.1 greatest integer, which is less than or equal to 36
hence Math.round(35.6) = 36

Eg2: Math.round(-37.8)
Step 1: add 0.5 i.e. �37.8 + 0.5 = -37.3
Step 2: find the floor ( i.e. value towards �ve infinity i.e. greatest integer less than
or equal to given value)
for -37.3 greatest integer, which is less than or equal to �38
( -37.3 > -38 , -37.3 < -37)
hence Math.round(-37.3) = -38

Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it�s as if we are adding 1 then doing a floor().

Math.round(values[x] + .5) means Math. floor ( values[x]+0.5+0.5) i.e. Math.floor(values[x]+1)

Given values are {-2.3, -1.0, 0.25, 4}; to find Math.round(values[x] + .5)value add +1 and apply floor.

Regards
Naresh
[ December 21, 2005: Message edited by: Naresh Kumar ]

Ranch Hand
Posts: 88
• Number of slices to send:
Optional 'thank-you' note:
Good explanation naresh....
regards,
sri.