12/07/2014

Just how much is PI in .Net?

 

<Silly blog article>

 

The .Net Framework includes a math library that includes a constant for PI. Just how much is PI?

The MSDN article says:

   image

 

The Visual Studio debugger says:

   image

The Visual Studio Autos and Locals windows say:

   image

And when written from Console.WriteLine:

   image

Or does it?  Reformatted with "R" WriteLine matches the value reported from the debugger.

   image

and for the final test:

   image

 

So who is right? According to Wikipedia the first 50 digits are 3.14159265358979323846264338327950288419716939937510. Based on that number the MSDN documentation describes a more accurate number than returned by Math.PI.

 

From the .Net double documentation: here and here

  • Double-precision numbers store an approximation of a real number.
  • Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
  • A Double value has up to 15 decimal digits of precision, although a maximum of 17 digits is maintained internally
  • When you work with floating-point numbers, remember that they do not always have a precise representation in memory.
  • The documentation's examples only have 18 digits, so PI as a double should be apx 3.14159265358979324

 

In the end…

3.14159265358979323846264338327950288419716939937510  Wikipedia
3.14159265358979323846   MSDN documentation for Math.PI
3.14159265358979324        What I would guess based on the documentation for doubles
3.1415926535897931          Returned in Visual Studio debugging tools
3.1415926535897931          Returned by the ToString() conversion through WriteLine formatted with "R"
3.14159265358979             Returned by the ToString() conversion through WriteLine

and just for fun… JavaScript says:
3.141592653589793

 

So…  for the important question… is the value of PI in the documentation for Math.PI wrong?  Smile

   image

 

</Silly blog article>

Now I have to get back to some real work!

.

1 comment:

Morgan said...

All this talk about pi is making me hungry!

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.