C# High double precision
Asked Answered
L

6

5

I'm writing a function that calculates the value of PI, and returns it as a double. So far so good. But once the function gets to 14 digits after the decimal place, it can't hold any more. I'm assuming this is because of the double's limited precision. What should I do to continue getting more numbers after the decimal place?

London answered 5/11, 2010 at 14:54 Comment(0)
G
16

I wouldn't do it in floating point at all.

Recall that your algorithm is:

(1 + 1 / (2 * 1 + 1)) *  
(1 + 2 / (2 * 2 + 1)) *  
(1 + 3 / (2 * 3 + 1)) *  
(1 + 4 / (2 * 4 + 1)) *  
(1 + 5 / (2 * 5 + 1)) *  
(1 + 6 / (2 * 6 + 1)) *  
(1 + 7 / (2 * 7 + 1)) *  ...

Every stage along the way you compute a fraction. Why not simply keep that fraction in its numerator / denominator form? The fraction you want to compute is:

(4 / 3) * 
(7 / 5) *
(10 / 7) *
(13 / 9) * ...

which is just 4 * 7 * 10 * 13 ... on the top and 3 * 5 * 7 * 9 on the bottom.

Get yourself a BigInteger class (one ships with the 4.0 framework in System.Numerics) and you can easily compute the numerator and denominator as big as you want. Then you just have the problem of converting the quotient to decimal. Well that's easy enough. Presumably you know how to do long division. Just implement a long division algorithm on the numerator and denominator that spits out the desired number of digits.

Gambill answered 5/11, 2010 at 15:12 Comment(0)
E
8

How much precision do you need?

Using decimal will give you roughly 28 decimal places:

decimal pi = 3.14159265358979323846264338327950288419716939937510m;
Console.WriteLine(pi);    // 3.1415926535897932384626433833

If that's not enough for you then you'll need to search for some sort of BigDecimal implementation, or look at other techniques for performing the calculation.

Eminent answered 5/11, 2010 at 14:59 Comment(0)
V
1

There are several libraries that let you work with arbitrary precision. One is W3b.sine, but several others are described on wikipedia.

Viens answered 5/11, 2010 at 15:2 Comment(0)
S
1

Try decimal instead of double. It can't store numbers as big as double, but I think it's got higher precision after the decimal. If you need more, you'll probably have to use a String.

Sicyon answered 5/11, 2010 at 15:2 Comment(2)
I wouldn't use a string - you'd have extra overhead of converting to/from numbers.Inexhaustible
Yeah, but if you need more than er, 28 digit precision, there are no built-in data types for that. IIRC C# does have some kind of class for values with (pretty much) unlimited size, and I'm pretty sure it would store them internally as a string...Sicyon
E
0

Yes, it's because the double's limited precision. There are a number of different ways to compute digits of pi. I would suggest asking your favorite search engine, "how to compute digits of pi".

Evangelical answered 5/11, 2010 at 15:0 Comment(0)
D
0

You can use the J# BigDecimal type, as suggested in this answer.

Decade answered 5/11, 2010 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.