• 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

very big calculations

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently working on a program that involves huge calculations like (345324523452345245345*4543563456456545456) .
Should BigDecimal suffice for this ? Also i sometimes need to convert them into double for example Math.pow(BigDecimalObject.doubleValue(),2)
Will there be a huge probability of an error during such conversions ?
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't mix floating point and arbitrary precision numbers.

If all your calculations are within integer arithmetic, you could use BigInteger. If not, BigDecimal is the way to go if you want arbitrary precision. If you only ever take integer powers of your number, you can use BigInteger.pow() or BigDecimal.pow().
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never use a power method for squares. You can lose on performance, and possibly precision, because there is a much simpler way to calculate squares.Use Strings rather than doubles as arguments to the BigDecimal constructor.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic