In Swift programs we often need to use mathematical methods. Fortunately many are included. With abs
we take an absolute value—this can be used as an index.
Funcs like sqrt are less often used. But when floor()
or ceil()
is needed, they simplify our Swift code. With Pow
we apply exponents.
Abs
Let us begin with abs()
. This method takes an absolute value. It always returns a positive number. It converts negative values to positive ones.
abs
we can convert any number into a possible array index (as for hashing).Abs
is often used in lower-level code. For most dictionary lookups, we do not need to implement a hash computation.let number = -10 print(number) // Use abs to make number positive. let result = abs(number) print(result) // An absolute value is always positive. let result2 = abs(result) print(result2)-10 10 10
Max
, minSometimes in a for
-loop we want to iterate until we reach the higher, or lower, of two values. This creates a safe boundary for the loop.
let small = 10 let big = 2000 // Compute max. let resultMax = max(small, big) print(resultMax) // Compute min. let resultMin = min(small, big) print(resultMin)2000 10
Sqrt
This method is part of Foundation. It requires a Double
argument. So we must cast Ints that we want to take the square roots of.
import Foundation // Compute square root with Foundation. let result = sqrt(Double(25)) print(result)5.0
Floor
This is a common mathematical function. It removes the fractional part of a number. So it changes 1.1 to 1.0. It will change 1.9 to 1.0 as well.
import Foundation // Compute floor for a Double. let number1 = 1.1 let floor1 = floor(number1) print("floor \(number1) = \(floor1)") // Floor removes the fractional part. let number2 = 1.9 let floor2 = floor(number2) print("floor \(number2) = \(floor2)")floor 1.1 = 1.0 floor 1.9 = 1.0
This method rounds numbers up, discarding a fractional part. So the ceiling of 0.1 is 1. Ceil is part of Foundation, so we must include the import statement.
import Foundation let number = 0.1 print(number) // Use ceil to remove the fractional part and round up. let result = ceil(number) print(result)0.1 1.0
Pow
This method takes exponents. It is part of Foundation. It requires a Double
and then returns a Double
. Here we take the square of 3.0 for a result of 9.0.
import Foundation // Use power of 2 on 3 (square it). let number = 3.0 let result = pow(number, 2) // Write results. print("\(number) squared = \(result)")3.0 squared = 9.0
This property returns a plus or minus value. If the Double
is negative, sign returns minus. If it is positive, the result is plus.
Int
with sign()
, we must use a cast to a Double
value. No Int
-accepting overload exists.import Foundation // A negative number has a sign of minus. let number = -1.0 let result = number.sign print("\(number), \(result)") // Convert Int to Double to use sign. // ... Positive numbers have sign bits of plus. let number2 = 200 let result2 = Double(number2).sign print("\(number2), \(result2)")-1.0, minus 200, plus
With a modulo division, we can determine if a number is even or odd. We must take special care on negative numbers. We can place this logic in funcs.
In the Fibonacci sequence, each number is equal to the sum of the two previous numbers. This sequence builds on itself. And it is found throughout nature.
Numbers are everywhere in our universe, even if unseen. With these math funcs in Swift, we manipulate numbers in known and common ways. This simplifies many programs.