Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a data type to hold 1 million+ digits in C#

I am calculating PI in C#, and so far its perfect except my data types are limiting me. If i use a double i get results like below.

k=0, delta= 3,14176587301587, pi=3,14176587301587

k=1, delta=-0,000173301147482709, pi=3,14159257186839

k=2, delta= 8,17736604635702E-08, pi=3,14159265364205

k=3, delta=-5,22954018637708E-11, pi=3,14159265358975

k=4, delta= 3,78997628626364E-14, pi=3,14159265358979

k=5, delta=-2,94045250629684E-17, pi=3,14159265358979

As you may notice, by my fourth run my digits are maxed. And decimal only helps for a little bit more, but i need A LOT. My algorithm loops and adds.

I thought about using a string, but my problem is, if i don't have a data type to hold those digits how do i get them into a string?

And i know people have done this before, i just wonder how...

Thanks for any advice!

like image 324
Landin martens Avatar asked Jan 27 '26 16:01

Landin martens


2 Answers

You can use Swensen.BigInt.dll assembly from CodeProject. If you need a .net 2.0 version, you can download it here.

like image 107
oxilumin Avatar answered Jan 30 '26 04:01

oxilumin


You could use BigInteger to store everything after the decimal point.

like image 35
Daniel Rose Avatar answered Jan 30 '26 04:01

Daniel Rose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!