0

Is it possible to get up to 1000 decimal digits in C#?

i need atleast 1000 decimal points of the value of pi for my program.

Haruka
  • 1
  • 1
  • 5
    If you only need 1000 digits, you can just look it up online and copy+paste into your program. – Mysticial Jul 08 '12 at 02:40
  • This won't help if you need to do any sort of arithmetic. Once it gets converted to a built in data type, you're going to lose most of that precision – Pete Baughman Jul 08 '12 at 02:44
  • Whenever someone asks a question like this, my first response is not to answer the how, but ask the why. Curiosity overwhelms me in this case. What on earth could you possibly **require** 1000 digits of precision for? – Chris Jul 08 '12 at 03:17
  • i need atleast 1000 decimal digits.but my problem is that the program lets the user input the number of decimal digits to show – Haruka Jul 08 '12 at 04:02

4 Answers4

3

Well you can use an array to simulate a number, for example, if you need to calcuate a(has 100000 digits) plus b(has 99999 digits):

 a:     a[100000]     a[99999]      a[99998]   ... a[1]           a[0]
+b:                   b[9999]       b[99998]   ... b[1]           b[0]
----------------------------------------------------------------------------
                                     ...   a[1]+b[1]+Carry      a[0]+b[0]
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
2

Yes, but not using the built in data types. Decimal is the best you can do and only has 28 to 29 significant digits.

You will need an arbitrary precision library, or you need to revisit your design and determine if you really need 1000 decimal digits.

If you are merely trying to calculate Pi out to 1000 digits, there are functions that you can use to calculate the nth digit of Pi. At that point, you can simply store the results in an Array

Pete Baughman
  • 2,996
  • 2
  • 19
  • 33
  • yes i need atleast 1000 decimal digits.but my problem is that the user will be the one to input how many decimal digits to show – Haruka Jul 08 '12 at 03:44
2

If you're still seeking an alternative to the other answers, check out the Bailey Borwein Plouffe formula for calculating the nth digit of Pi. You can use it to calculate the first 1000 digits.

enter image description here

Austin Henley
  • 4,625
  • 13
  • 45
  • 80
  • @BenVoigt Indeed. I'm mildly amazed that half the answers recommend using that digit extraction algorithm - which only works for *binary* digits. – Mysticial Jul 08 '12 at 03:29
  • @Mysticial: Well, it appears that Plouffe did somewhere publish a formula for the decimal digits of Pi... but that formula isn't it. In fact, I don't see how that formula gives binary or even hexadecimal digits (each fractional place should be `16^-k` (the place value) multiplied by some integer in the range 0-15 (the digit itself), and I see the place value in the above formula, but what should be a digit doesn't appear to be integral. – Ben Voigt Jul 08 '12 at 03:35
  • 1
    @BenVoigt The algorithm for binary digits is quasi-linear. But the algorithm for decimal digits is either quadratic or cubic. (I can't remember which.) So that's why nobody uses it. I've implemented the BBP for binary digits. It's actually very tricky as it abuses power modulus to "skip" all the preceding digits. IMO, it requires a fairly strong math background to implement. – Mysticial Jul 08 '12 at 03:39
  • @BenVoigt Come to the lounge if you wanna discuss this further. Since it quite literally is in my area of expertise. lol – Mysticial Jul 08 '12 at 03:40
1

You need to use abritrary precision arithmetic. I'm not sure about native C# libraries, but GMP says it has a C# wrapper.

You can also use J#'s BigDecimal.

Here's a similar question.

Community
  • 1
  • 1
Antimony
  • 37,781
  • 10
  • 100
  • 107