NOTE
Mr. Gordon generated this concept summary and the related examples using ChatGPT (here are the prompts given and responses received).
He then reviewed and edited the results for clarity and accuracy.
This page is intended as a reference (please bookmark for future use).
TIP
It is expected at this early point in the course that you will not understand all of the code segments of concepts explained below!
Examples that show how to use some constants (such as ) will be useful now.
Some code examples techniques below will be useful in the future.
1. Pi (π)
What it is:
Circle circumference divided by diameter. Use for radians and circle formulas.
Example 1 (Circle circumference):
let r = 4.0
let C = 2 * Double.pi * r
print(C) // 25.1327...Example 2 (Degrees → Radians):
let degrees = 60.0
let radians = degrees * Double.pi / 180.0
print(radians) // 1.0472...Example 3 (Sector area):
let r = 5.0
let theta = 30.0 * Double.pi / 180.0 // 30° in radians
let sectorArea = 0.5 * r * r * theta
print(sectorArea) // 13.0899...2. Euler’s Number (e)
What it is:
Base of natural logarithms; continuous growth/decay. Swift doesn’t ship a named constant; compute with exp(1.0).
Example 1 (Constant e itself):
import Foundation
let e = exp(1.0)
print(e) // 2.71828...Example 2 (Continuous compounding):
import Foundation
let A0 = 1000.0 // initial amount
let k = 0.07 // 7% per year (continuous)
let t = 5.0 // years
let A = A0 * exp(k * t)
print(A) // 1419.07...Example 3 (Half-life):
import Foundation
let halfLife = 10.0 // years
let k = log(2.0) / halfLife
let t = 15.0
let remaining = exp(-k * t) // fraction remaining after 15 years
print(remaining) // 0.3535...3. Infinity
What it is:
Represents values larger than any finite number.
Example 1 (Initialize “worst” for a minimum search):
var best = Double.infinity
for value in [9.2, 5.4, 7.1] {
best = min(best, value)
}
print(best) // 5.4Example 2 (Asymptote check):
let x = 0.0
let y = 1.0 / x // y becomes +inf (division by 0.0 in Double yields infinity)
print(y.isInfinite) // trueExample 3 (Bounding box placeholder in optimization):
var lo = -Double.infinity
var hi = Double.infinity
// later: tighten lo/hi as you evaluate constraints4. NaN (Not a Number)
What it is:
Represents an undefined result (e.g., 0/0). Any math with NaN usually stays NaN.
Example 1 (Vertical line slope):
func slope(x1: Double, y1: Double, x2: Double, y2: Double) -> Double {
let dx = x2 - x1
let dy = y2 - y1
return dx == 0 ? Double.nan : dy / dx
}
print(slope(x1: 2, y1: 1, x2: 2, y2: 5).isNaN) // trueExample 2 (Filtering data):
let raw: [Double] = [2, .nan, 5, 7, .nan, 3]
let cleaned = raw.filter { !$0.isNaN }
print(cleaned) // [2.0, 5.0, 7.0, 3.0]Example 3 (Guard against NaN results):
let value = (0.0 / 0.0) // NaN
let safe = value.isNaN ? 0.0 : value
print(safe) // 0.05. Zero
What it is:
Convenience Double.zero is exactly 0.0.
Example 1 (Accumulator start):
let dailyChanges = [+30.0, -12.0, +5.0, -7.0]
let net = dailyChanges.reduce(.zero, +)
print(net) // 16.0Example 2 (Vector zero):
let vx = Double.zero
let vy = Double.zero
print(vx, vy) // 0.0 0.0Example 3 (Default to zero):
let maybeScore: Double? = nil
let score = maybeScore ?? .zero
print(score) // 0.06. Greatest Finite Magnitude
What it is:
Largest finite Double; useful as an initial bound.
Example 1 (Track minimum error):
var bestError = Double.greatestFiniteMagnitude
for guess in stride(from: -5.0, through: 5.0, by: 0.5) {
let error = abs(guess * guess - 9.0) // distance from 3^2
if error < bestError { bestError = error }
}
print(bestError) // 0.0Example 2 (Sentinel):
struct Result { let value: Double }
let none = Result(value: .greatestFiniteMagnitude) // means “unset yet”Example 3 (Clamp):
let x = 1e400 // overflows to inf in Double literals
let clamped = x.isFinite ? x : Double.greatestFiniteMagnitude
print(clamped.isFinite) // true7. Least Normal & Least Nonzero Magnitude
What they are:
leastNormalMagnitude: smallest normal positive Double.leastNonzeroMagnitude: smallest positive Double representable.
Example 1 (Avoid divide-by-zero):
let eps = Double.leastNonzeroMagnitude
func safeDivide(_ a: Double, _ b: Double) -> Double {
return a / max(abs(b), eps)
}
print(safeDivide(5, 0)) // a very large finite numberExample 2 (Numeric tolerance):
let eps = Double.leastNormalMagnitude
let nearZero = 1e-320
print(abs(nearZero) < eps) // true -> treat as 0 for comparisonsExample 3 (Stability in formulas):
let eps = Double.leastNonzeroMagnitude
let denom = (1.0 - 1.0)
let fraction = 1.0 / max(abs(denom), eps) // avoids inf
print(fraction.isFinite) // true8. ULP of One
What it is:
ulpOfOne is the spacing between consecutive Double values around 1.0. Good for tolerance checks.
Example 1 (Equality with tolerance):
let a = 0.1 + 0.2
let b = 0.3
let tol = 10.0 * Double.ulpOfOne
print(abs(a - b) < tol) // trueExample 2 (Rounding threshold experiment):
let tol = Double.ulpOfOne
let x = 1.0 + 0.5 * tol
print(x == 1.0) // true (too small to change representation)Example 3 (Iterative methods stop rule):
var prev = 1.0
var curr = 0.5
let tol = 1000 * Double.ulpOfOne
while abs(curr - prev) > tol {
prev = curr
curr = 0.5 * (curr + 2.0 / curr) // Newton step for sqrt(2)
}
print(curr) // close to sqrt(2)9. Tau (τ = 2π)
What it is:
Full turn in radians. Not a built-in symbol, but easy to define.
Example 1 (Define once):
let tau = 2 * Double.piExample 2 (Angles modulo a full turn):
let angle = 7.5 // radians
let tau = 2 * Double.pi
let wrapped = angle.truncatingRemainder(dividingBy: tau)
print(wrapped) // angle reduced to [0, τ)Example 3 (Circle frequency):
// sine wave with frequency f: sin(τ f t)
let f = 2.0 // Hz
let t = 0.25 // seconds
let tau = 2 * Double.pi
let y = sin(tau * f * t) // sin(π) = 0
print(y) // ~0.0