163

I am trying to find a way to include the PI constant in my Swift code. I already found help in another answer, to import Darwin which I know gives me access to C functions.

I also checked the Math package in Darwin and came across the following declaration:

var M_PI: Double { get } /* pi */

So, I assume there is a way to use PI in the code, I just don't know how...

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
nburk
  • 22,409
  • 18
  • 87
  • 132

3 Answers3

308

With Swift 3 & 4, pi is now defined as a static variable on the floating point number types Double, Float and CGFloat, so no specific imports are required any more:

Double.pi
Float.pi
CGFloat.pi

Also note that the actual type of .pi can be inferred by the compiler. So, in situations where it's clear from the context that you are using e.g. CGFloat, you can just use .pi (thanks to @Qbyte and @rickster for pointing that out in the comments).

For older versions of Swift:

M_PI is originally defined in Darwin but is also contained in Foundation and UIKit, so importing any of these will give you the right access.

import Darwin // or Foundation or UIKit

let pi = M_PI

Note: As noted in the comments, pi can also be used as unicode character in Swift, so you might as well do

let π = M_PI

alt + p is the shortcut (on US-keyboards) that will create the π unicode character.

nburk
  • 22,409
  • 18
  • 87
  • 132
  • 5
    As Swift supports Unicode, you could also do `var π = M_PI` – Cyrille Oct 12 '14 at 10:21
  • 23
    As pi is a constant, it seems perverse to declare it as a `var`. Surely `let pi = M_PI`? – Grimxn Oct 12 '14 at 10:53
  • @Cyrille sounds kinda cool but wouldn't that mean copy-paste every time we want to reference it? – Stavash Oct 12 '14 at 11:23
  • 1
    Don't know about the standard keyboard, but on my custom keyboard mapping (made with Ukulele) that's simple as typing alt+P, along with a slew of other custom characters. – Cyrille Oct 12 '14 at 11:24
  • 2
    You don't need anything special - Alt-p is pi by default. – Grimxn Oct 12 '14 at 20:03
  • 3
    Alt-P on an US keyboard, I assume? Definitely not on mine (where it invokes print window). Please, please, don't write random unicode characters in code. I don't want my colleagues to go googling how to write π everytime they find it in code. – Sulthan Dec 12 '14 at 12:10
  • 1
    you don't need to `import Darwin` to get `M_PI` – ToddB Dec 30 '14 at 18:42
  • well, `M_PI` is in the package `Darwin`, **but** it is also in `UIKit`. so you don't get it for free, but you need to import either `UIKit` or at the very least `Darwin` to use it. – nburk Dec 30 '14 at 18:48
  • It's not the Alt key, it's the Option key. It's Option-P! – Gary Makin Mar 24 '15 at 08:16
  • :D well, the `option` key is **labelled** with `alt`, so I guess it's fair to say that `alt + p` is the requested shortcut ;) @GaryMakin – nburk Mar 24 '15 at 08:23
  • 2
    Just a note: If the type of Pi can be inferred you can also use `.pi` instead of the long form. E.g. `let x = CGFloat(42) * .pi` – Qbyte Jun 22 '16 at 08:54
  • 1
    +1 to @Qbyte's comment. Type inference is the "big idea" behind making `.pi` a static member of *all* the floating point types: if you're calling an API that takes a floating point value, you can just pass (or do math with) `.pi` and not have to care which type; e.g. `maxMarker.center = CGPoint(x: .pi / 2, y: 1)` – rickster Jun 27 '16 at 19:25
  • But what about the precomputed constants M_PI_2, M_PI_4? Should we define them for each floating type? They were really useful in programs doing a lot of geometry stuff! – nbloqs Jan 11 '17 at 17:04
  • @Sulthan You're typing command-P.  The alt/option key is the middle one, between the command and control keys.  If you have an Apple-manufactured keyboard, it should be prominently labelled “option” with a smaller “alt” label. – Slipp D. Thompson May 16 '17 at 11:55
15

import Darwin is not needed all M_x are visible with the import Foundation

( Xcode Version 6.4 (6E35b) )

nburk
  • 22,409
  • 18
  • 87
  • 132
Guyvo
  • 171
  • 1
  • 5
4

warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

surprisingly, .pi also works fine. M_PI is deprecated as of Swift 4.2.1, Xcode 10.1, which is the current version I am using. SO, Use .pi, or Double.pi

Sree
  • 309
  • 3
  • 7