• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

round method

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic