aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.round...(for -ve no.) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.round...(for -ve no.)" Watch "Math.round...(for -ve no.)" New topic
Author

Math.round...(for -ve no.)

ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
Hi,

public class math
{
public static void main(String a[])
{
System.out.println(Math.round(-2.5));// o/p is -2....line 1
System.out.println(Math.round(-2.4));// o/p is -2....line 2
System.out.println(Math.round(-2.6));// o/p is -3....line 3
System.out.println(Math.round(2.5)); // o/p is 3...as expected.
System.out.println(Math.round(2.4)); // o/p is 2...as expected.
System.out.println(Math.round(2.6)); // o/p is 3...as expected.
}
}
round returns a long for double, returns an int for float. (closest int or long value to the argument)
The result is rounded to an integer by adding � , taking the floor of the result, and casting the result to type int / long.
NOw...look at the o/p of line 1 and line 3 (-2.5 is giving -2 whereas line -2.6 is giving -3.) . As fer as in +ve no. this is not the rule. In positive nos. both 2.5 and 2.6 will give 3.Now look at line 2 also.
same problem here.
Can any1 tell me what is real rule for -ve no's roundness..
I am simply not geting any rule??
Thanks In Advance.
<marquee> Ratul Banerjee </marquee>
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Ratul.
Math.round() returns the integral value (int or long) that is closest to the input value. If that input value is equidistant from its neighboring integral values (e.g., -2.5, +104.5), Math.round() rounds up, towards positive infinity. Hence Math.round( -2.5 ) yields -2, and Math.round( +2.5 ) yields 3.
Art
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
Thanks Art
can u tell me then why line 3 is showing -3 on the stnd. o/p.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Because -3 is the int that is closer to the float -2.6. As I mentioned earlier, as a general rule, Math.round() returns the integral value that is closer to the input value. The int closer to -2.6 is -3. Similarly, the int closer to +2.6 is +3, so Math.round( +2.6 ) = 3.
Now that we've established that round() returns the integral value that's closer to the input, the question presents itself, what is the behavior if the input value is equidistant from its neighboring integral values? Which int is "closer" then? That is, if float f = ( 2n + 1 ) / 2 for integral n, what is Math.round( f )? The answer is, for these special cases where round()'s input is "something-point-five", round() rounds up towards positive infinity.
Maybe this illustration will help:

If this hasn't cleared round() up for you, Ratul, let me know.
Art
shadow liu
Ranch Hand

Joined: Mar 19, 2001
Posts: 33
see the src code of Math.round ( i just pick the float->int version):
public static int round(float a) {
return (int)floor(a + 0.5f);
}
so round(-2.4f)=floor(-1.9f)=-2
round(-2.5f)=floor(-2.0f)=-2
round(-2.6f)=floor(-2.1f)=-3
HTH.
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
THANKS ART ...It is clear now.
Thanks shadow
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Math.round...(for -ve no.)