PowerTip: Displaying the value of pi


Summary: Learn to obtain the value of pi.

Hey, Scripting Guy! Question How can I obtain the value of pi?

Hey, Scripting Guy! Answer

a. [math]::pi

b. 22/7

Comments (10)

  1. jrv says:

    @DF01

    This is not an approximation.  It is a legitimate way to calculate PI.  There are many ways to do it.

    ((4*[math]::Atan(1/5))- [math]::atan(1/239)) * 4

    It will calculate PI as far as [math]::pi will.  [math]::pi may use this formula or one of the many other formulas for PI.  The plus with this formula is that it takes good advantage of the floating point processor. Simple ratios will not.

    No ratio can accurately calculate PI.  Ratios can be devised that will come ever closer to PI but will never get there.  The nexr useful ratio is likely to be published soon. It is likely to be a big number.  PowerShell cannot approximate well beyond 355/113 unless we use a 128 bit or larger FPP.

    18:13 PS>((4*[math]::Atan(1/5))- [math]::atan(1/239)) * 4

    3.14159265358979

    18:13 PS>[math]::pi

    3.14159265358979

    as you can see they are identical.

  2. I don't think 22/7 is a good idea to obtain the value of pi but I think using the function there is.

  3. jrv says:

    @Russ – that is good but can we calculate with it.

    Well – PowerShell is very smart about words and language and can use your little helper very easily.

    Proof of the 'Russ' conjecture:

    "$('How I wish I could calculate pi better'.split(' ')|%{$_.length})"

  4. jrv says:

    Machin's formula is much, much closer than 22/7

    ((4*[math]::Atan(1/5))- [math]::Atan(1/239)) * 4

  5. Anonymous says:

    @jrv That was AWESOME!

  6. jrv says:

    Forgive me for the digression but I do think, as Ed was focusing on in the post, that PowerShell can make for a very good math laboratory.  It can be very useful for kids and college students alike.  

    With the [math] type we can explore many of the advanced elements of fundamental math like trigonometry, calculas, number theory and even set theory.  Using MS Graph I suspect we can even explore vector algebra and other things like it.

  7. DF01 says:

    The best known approximation of Pi is 86953 / 27678. See en.wikipedia.org/…/113. That said, [math]::pi is much more accurate.

  8. M Baechtel says:

    Use 355/113 its good to the 7th place. Just think 113355 use the last 3 div by the first 3,

    If you use 22/7 its only good to the 3rd place.

  9. Russ Pitcher says:

    22/7!?! That's a terrible approximation!

    I've always liked the mnemonic "How I wish I could calculate pi better".  The number of letters in each word gives you pi to 8 figures: 3.1415926

Skip to main content