I'm well aware that a Double has only so many bits of precision, but we should still try to achieve high accuracy if possible. So I certainly do not expect to see this in the official .NET 4 system library.
// Summary:
// Represents the ratio of the circumference of a circle to its diameter,
// specified by the constant, π.
public const double PI = 3.14159;
Why only 6 digits? It would be easy and free to use an accurate value. I'm not doing scientifi开发者_StackOverflow社区c work in .NET, but I'm sure others do and they are in for a surprise when Pi is inaccurate. Same goes for E.
Edit: This tuned out to be about reflection of constants in Visual Studio. See followup question
Copy and pasted from Math.cs in the .NET 4.0 Reference Source:
public const double PI = 3.14159265358979323846;
public const double E = 2.7182818284590452354;
No idea what you are looking at.
It was reverse-engineered from a follow-up question that you looked at the auto-generated text that was created from the assembly metadata when you use the Go To Definition context menu item. Yes, the code that generates this text appears to use the default %f formatting on public double constant values. Quite rare btw, there are not a lot of public constants that are double in the .NET framework. You can file a feedback report at connect.microsoft.com
A quick test shows that Console.WriteLine(Math.PI)
outputs 3.14159265358979
so how can this be true? Are you saying ToString()
adds extra digits by magic?
You can use Pi = Math.acos(-1)
.
More seriously, do not do math in .NET unless you really know what you're doing.
精彩评论