# Thread: using the BigDecimal class

1. Member Join Date
Oct 2011
Posts
2
Rep Power
0

## using the BigDecimal class

I am trying to write a program using the BigDecimal class, but I have a very small understanding of their functioning. There's nothing too special about my program...

Using this formula:
e = (1 / 0!) + (1 / 1!) + (1 / 2!) + ...

I essentially want to display e for every computation (1/n!) until e has a scale of at least 100.

I thought that as you continued to divide and add and repeat over and over the scale would increase as you continue to compute the infinite number e as it shrank, but in my program the scale maxes out at 51,and eventually just repeats at scale() = 50. Even if I try to use the setScale(int) method, the scale won't actually change. I can call setScale(100) and scale() back to back and the scale will not display as 100, why wouldnt it change?

This is my code right now. I know there may be some issues other than the BigDecimal problem, but even in super simple tests of BigDecimal i cant get it to work how I want.
Java Code:
```//Main class
public static void main(String[] args) {

double N = 1;
double tempE = 1;
BigDecimal e = new BigDecimal(tempE);

while(e.scale() < 100)
{
tempE += (1/(double)calcFactorial(N));
e = new BigDecimal(tempE);

System.out.println(e + "    Scale: " + e.scale());
++N;

}

}

// Returns the factorial of a given number
public static double calcFactorial(double number){

double factorial = 1;
double currNumber = 1;

while(currNumber <= number){
factorial *= currNumber++;
}

return factorial;
}```

Example output:
2 Scale: 0
2.5 Scale: 1
2.666666666666666518636930049979127943515777587890 625 Scale: 51
2.708333333333333037273860099958255887031555175781 25 Scale: 50
2.716666666666666341001246109954081475734710693359 375 Scale: 51
2.718055555555555447000415369984693825244903564453 125 Scale: 51
2.71825396825396836675281520001590251922607421875 Scale: 47
2.718278769841270037233016410027630627155303955078 125 Scale: 51
2.718281525573192247691167722223326563835144042968 75 Scale: 50
2.718281801146384513145903838449157774448394775390 625 Scale: 51
2.718281826198492900914516212651506066322326660156 25 Scale: 50
2.718281828286168710917536373017355799674987792968 75 Scale: 50
2.718281828446759362805096316151320934295654296875 Scale: 48
2.718281828458230187095523433526977896690368652343 75 Scale: 50
2.718281828458994908714885241352021694183349609375 Scale: 48
2.718281828459042870349549048114567995071411132812 5 Scale: 49
2.718281828459045534884808148490265011787414550781 25 Scale: 50
2.718281828459045534884808148490265011787414550781 25 Scale: 50
2.718281828459045534884808148490265011787414550781 25 Scale: 50
2.718281828459045534884808148490265011787414550781 25 Scale: 50
2.718281828459045534884808148490265011787414550781 25 Scale: 50
......
(and continues to repeat the last line in an infinite loop, as if the precision is getting cut off at this point instead of expanding the scale)  Reply With Quote

2. ## Re: using the BigDecimal class

Java Code:
`93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L`
This is 100! and cannot be calculated by your calcFactorial( ... ) method because it uses doubles ...For the factorials it can calculate, it only calculates them up to 15 decimals of accuracy.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Oct 2011
Posts
2
Rep Power
0

## Re: using the BigDecimal class

If calcFactorial(...) will only return a variable with up to 15 decimals of accuracy, will that reduce the BigDecimals ability to reach a greater precision? I am also looking for suggestions on how to right a factorial method that will return a variable with greater precision if this will help.  Reply With Quote

4. ## Re: using the BigDecimal class Originally Posted by jopeters If calcFactorial(...) will only return a variable with up to 15 decimals of accuracy, will that reduce the BigDecimals ability to reach a greater precision? I am also looking for suggestions on how to right a factorial method that will return a variable with greater precision if this will help.
Use the BigInteger class together with the BigDecimal class and don't calculate each term from scratch, i.e. if you have found n! calculating (n+1)! can be done with one multipliation, i.e. (n+1)! == (n+1)*n!

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•