Meaningless Drivel is fun!
The moose likes Java in General and the fly likes BigDecimal is driving me crazy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigDecimal is driving me crazy" Watch "BigDecimal is driving me crazy" New topic

BigDecimal is driving me crazy

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

i wrote a class to compute e

this works fine but only to a certain precision. so i am trying to use BigDecimal. at first i was getting an exception when i divided but i learned i had to specify the rounding. i did that but now i get 2 as an answer

Visit my download page
Paul Clapham

Joined: Oct 14, 2005
Posts: 19758

The first thing that I thought when I read that code was "How does BigDecimal.divide know how many decimal places to use?"
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

yeah i think that might be the problem. i guess i was assuming a large default value for that. this is the first time i have tried using this class.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

Off topic, but I have two small improvements for you:

Use BigDecimal.ONE and BigDecimal.valueOf(j). The former will definitely reuse an existing object, the latter will probably do so as well. Currently, all values from 0 to 10 are cached; that already saves you up to 10 extra objects per loop.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: BigDecimal is driving me crazy
It's not a secret anymore!