C # High double precision

I am writing a function that calculates a PI value and returns it as a double. So far, so good. But as soon as the function receives up to 14 digits after the decimal point, it can no longer be held. I guess this is due to double limited accuracy. What to do to continue to receive more digits after the decimal point?

+5
source share
6 answers

I would not do this in a floating point at all.

Recall that your algorithm:

(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)) *  ...

Each step along the way you calculate the fraction. Why not just keep this fraction in the form of a numerator / denominator? The fraction you want to calculate:

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

which is only 4 * 7 * 10 * 13 ... above and 3 * 5 * 7 * 9 below.

BigInteger ( 4.0 System.Numerics), , . . , . , , . , .

+16

?

decimal, 28 :

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

, - BigDecimal .

+8
+1

decimal . , double, , . , , , .

+1

, , . pi. , " pi".

0

You can use the J # type BigDecimalas suggested in this answer .

0
source

All Articles