Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Art
can u tell me then why line 3 is showing -3 on the stnd. o/p.
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS ART ...It is clear now.
Thanks shadow
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic