• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

round method

 
Mamta Swain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
As per Java Specification for round method of Math
If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
So I tried these 2 below lines.
System.out.println(Math.round(Integer.MIN_VALUE-1));
System.out.println(Math.round(Integer.MAX_VALUE+1));
And the result is giving
2147483647
-2147483648
So it seems to me that
If the argument is less than the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MAX_VALUE.
If the argument is greater than the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MIN_VALUE.
Could anybody explain the above.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the reason is the following
the maximum integer value is 0x7FFFFFFF
when you add 1 (0x00000001) you get 0x80000000 which is Integer.MIN_VALUE.
the same goes for the opposite. The thing to remember is that there is no overflow with integer.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Mamta Swain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Valentin.
I guess I forgot to put my mind into bit manipulation. It becomes really easy to understand that way.
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameters you passed in are not really less than
Integer.MIN_VALUE or greater than MAX_VALUE. They are
just result of integer operation being promoted to float.
As Val mentioned, integer operations are wrapped around.
So, you should do something like followings to verify the
API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic