How to get more exact Pi constant? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to get more exact Pi constant?

Pi number is a Math class constant variable. In C# Pi is double type. Max digits of double is 14. So Pi = 3,14159265358979. For example somebody wants get 30 digits. Is it possible in c#?

23rd Aug 2018, 3:14 AM
Horman Lewis
Horman Lewis - avatar
6 Answers
+ 2
You should use decimal.and executed a const for pi with 30 digits . Then use it every where you want .
23rd Aug 2018, 4:41 AM
Shima Mohkami
Shima Mohkami - avatar
+ 4
Doubles have 15-16 digits of precision. Decimals have 28-29: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/floating-point-types-table 30 seems to be just out of range. This would be easy enough to test; try setting 30 total digits and see if the last digit changes on any value 0-9. Edit: Note, the 29th digit is also likely to be unreliable.
23rd Aug 2018, 5:05 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
You should use decimal.and executed a const for pi with 30 digits . Then use it every where you want . const decimal pi = (pi in 30 digits); (you can find pi in internet)
23rd Aug 2018, 4:44 AM
Shima Mohkami
Shima Mohkami - avatar
23rd Aug 2018, 4:56 AM
Shima Mohkami
Shima Mohkami - avatar
+ 2
i think you can do it easily by constant decimal
23rd Aug 2018, 4:41 AM
Shima Mohkami
Shima Mohkami - avatar
0
thanks for answer Zahra Mohkami but what if I need 300 digits or 1000? It will take a lot of space of programm code. You give me direction to thinking. I will calculate pi and put it into decimal variable.
23rd Aug 2018, 5:04 AM
Horman Lewis
Horman Lewis - avatar