Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal is driving me crazy

 
Randall Twede
Ranch Hand
Posts: 4409
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4409
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic