Results 1 to 4 of 4
Thread: using the BigDecimal class
- 10-07-2011, 04:50 AM #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)
- 10-07-2011, 05:12 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: using the BigDecimal class
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.Java Code:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-07-2011, 05:47 AM #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.
- 10-07-2011, 05:56 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: using the BigDecimal class
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
More sensible BigDecimal toString()?
By morello in forum Advanced JavaReplies: 3Last Post: 09-29-2011, 04:08 PM -
Negating a BigDecimal
By ryanmk54 in forum New To JavaReplies: 5Last Post: 05-16-2011, 06:23 PM -
BigDecimal and BigInteger
By jon80 in forum New To JavaReplies: 8Last Post: 11-28-2010, 11:15 AM -
Using BigDecimal
By doymand in forum New To JavaReplies: 2Last Post: 10-30-2010, 09:25 AM -
BigDecimal question
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks