How to get mathemical PI constant in Swift
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.
What's the difference between M_PI and M_PI_2?
Use Double.pi / 2
for M_PI_2 and Double.pi
for M_PI.
You can also use Float.pi
and CGFloat.pi
.
In Swift 3 & 4, pi is defined as a static variable on the floating point number types Double
, Float
and CGFloat
.
M_P is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting in Swift 3.1
Well, follow the instructions of the error message:
let center = containerShapeView.center
let startAngle = CGFloat(0.0)
let endAngle = CGFloat(Double.pi) * 2
let radius = containerShapeView.bounds.width * 0.21
Or better yet, to avoid the unecessary conversions:
let center = containerShapeView.center
let startAngle: CGFloat = 0
let endAngle = CGFloat.pi * 2
let radius = containerShapeView.bounds.width * 0.21
How does Swift deal with constant values into mathematical functions?
The compiler is likely to inline the call, and once the call is inlined, it's also likely to perform constant-folding in order to do the calculation at compile-time.
So yes, with optimizations enabled, it's reasonably likely that radian(360)
will be compiled into 6.283184
. But the only way to verify this is to actually look at the resulting assembly of an optimized build.
If you want to make this even more likely, you can stick the undocumented @inline(__always)
attribute on your function. That will force it to always inline the function. But this is probably unnecessary.
Math.Pi constant is wrong
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
What is M_PI/Double.pi used for, and why do we use it? Spritekit
Double.pi
is used in this sample because rotate(byAngle:duration:)
takes an angle in radians. 2π radians is equal to one full rotation, or 360 degrees. The code you included is creating a rotation action telling the plane to rotate one full rotation every 3.8 seconds. Removing the Double.pi
is just causing it to rotate a smaller angle every 3.8 seconds.
As for the difference between M_PI
and Double.pi
, they're basically the same. The latter is newer and matches similar constants on other types, such as CGFloat.pi
. These other constants allow you to get correctly-typed values of π for other numeric types. M_PI
was always a Double
, but CGFloat.pi
is a CGFloat
, Float.pi
is a Float
, etc.
In fact, in your code, switching to CGFloat.pi
might allow you to remove the CGFloat
cast around your angle parameter, like so:
let OrbitCenter = SKAction.rotate(byAngle: -2 * CGFloat.pi, duration: 3.8)
What's the difference between pi and M_PI in objc
pi
is defined in the "CarbonCore.framework" headers as
extern const double_t pi __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_NA, __IPHONE_NA);
but marked as "deprecated". I assume that it is a relict from older Carbon frameworks.
M_PI
is defined as a macro
#define M_PI 3.14159265358979323846264338327950288
in math.h
and part of the POSIX standard.
The values are identical, but you should use M_PI
for portability reasons.
(And for Swift, see How to get mathemical PI constant in Swift)
Pi Output In Swift
To represent 90
degrees as π/2
, what you want to do is
consider the fraction of
degrees
over 180 (i.e. numerator ofdegrees
and denominator of180
);reduce this fraction (i.e. divide both the numerator and denominator by the greatest common factor); in the case of
90/180
, the greatest common factor is, in fact,90
, yielding a reduced numerator of1
and a reduced denominator of2
;the result as a string representation of the fraction, i.e. something like
"\(reducedNumerator)π/\(reducedDenominator)"
Obviously, if either the numerator or denominator are
1
, then you can suppress that portion of the string, but hopefully you get the basic idea.
Take a crack at that.
Math.Pi constant is wrong
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
Related Topics
How to Check If a String Contains Another String in Swift
Structure VS Class in Swift Language
Getting a "This Application Is Modifying the Autolayout Engine from a Background Thread" Error
How to Use Swift 4 Codable in Core Data
Segue and Button Programmatically Swift
Alamofire Get API Request Not Working as Expected
How to Resolve "Ambiguous Use Of" Compile Error With Swift #Selector Syntax
Swift - How to Convert String to Double
Swiftui Hstack With Wrap and Dynamic Height
Do Swift-Based Applications Work on Os X 10.9/Ios 7 and Lower
Nsurlsession Concurrent Requests With Alamofire
Photopicker Discovery Error: Error Domain=Pluginkit Code=13
Why Are Emoji Characters Like 👩👩👧👦 Treated So Strangely in Swift Strings