Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using BigDecimal

807600Oct 9 2007 — edited Oct 10 2007
My factorial is hitting a wall, and it is a huge decimal (I put it under 1). How do I use BigDecimal? Here is what I am using to solve these factorials:
    public static double factorial(double n) {
        if      (n <  0)  throw new RuntimeException("Can't be negative stupid.");
        else if (n > 250) throw new RuntimeException("Number too big at the moment! :(");
        else if (n == 0)  return 1;
        else              return (1 / (n * factorial(n-1)));
 double z = 200
factorial(z);
I get errors because I don't think double can handle something so large. How do I change that snippet of code to BigDecimal, rather how do I use it? The API sort of tells me this is much different from regular number types. Also, can a BigDecimal have a value in the ones or tens place, like 23.839281.... or is it strictly the decimal end, 0.839281... ?

Edited by: kavon89 on Oct 9, 2007 10:49 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 7 2007
Added on Oct 9 2007
5 comments
711 views