• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting result of -1,1,0 as a result of compare operation.

Now i want to add this value to db column value, as the above value is in int type thats why now using the Integer.

SnapShot of the code:


Question: Is doing addition operations recommended through int rather than BigDecimal because it will not loose its precision ?

Question: does intValue() loose it precision after certain range.

if not i can change the above code to like this.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All integer types (byte, char, short, int and long) never lose any precision, but they have limited range. If this is not going to be a problem using ints or longs is just fine.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will they loose precision after the limited range. so how much is the limited range?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure the API documentation tells you what the value of (for example) Integer.MAX_VALUE is.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:will they loose precision after the limited range. so how much is the limited range?



If you try to assign a value to a primitive type that is out of its range the JVM will yell at you. You can't assign a value to a primitive that is outside of its range unless you explicitly cast it to the desired type, but it will result in loss of precision.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add an int to an int, and the resulting value would become larger than Integer.MAX_VALUE, it will wrap - becoming negative. If you believe this is a possibility, use long. If the same issue can occur for long (becoming larger than Long.MAX_VALUE), BigInteger and BigDecimal are the only options.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i will go with BigDecimal. Integer.Max_value has a limit of 2147483647.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic