Mamta Swain

Ranch Hand

Posts: 30

posted 15 years ago

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.

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

Sheriff

Posts: 7610

posted 15 years ago

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

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

Posts: 30

Nain Hwu

Ranch Hand

Posts: 139

posted 15 years ago

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.

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.

Consider Paul's rocket mass heater. |