wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes round method 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 "round method" Watch "round method" New topic
Author

round method

Mamta Swain
Ranch Hand

Joined: Oct 11, 2001
Posts: 30
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

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mamta Swain
Ranch Hand

Joined: Oct 11, 2001
Posts: 30
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

Joined: Sep 16, 2001
Posts: 139
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.
 
 
subject: round method