Math. 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.
Func notes. 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.
And It returns positive numbers unchanged. With abs we can convert any number into a possible array index (as for hashing).
Tip Abs is often used in lower-level code. For most dictionary lookups, we do not need to implement a hash computation.
let number = -10
// Use abs to make number positive.
let result = abs(number)
// An absolute value is always positive.
let result2 = abs(result)
Max, min. Sometimes 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.
Detail This returns the larger of two numbers. It handles negative numbers. Here it returns 2000 because that was the larger argument.
Detail Returns the smaller of two numbers. In this example we get 10 because 2000 is larger than 10.
let small = 10
let big = 2000// Compute max.
let resultMax = max(small, big)
// Compute min.
let resultMin = min(small, big)
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.
Note Taking a square root is not a common requirement in most programs. So including Foundation is not a big problem.
// Compute square root with Foundation.
let result = sqrt(Double(25))
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.
// 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
Ceil. 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.
let number = 0.1
// Use ceil to remove the fractional part and round up.
let result = ceil(number)
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.
// 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
Sign. This property returns a plus or minus value. If the Double is negative, sign returns minus. If it is positive, the result is plus.
Tip To test an Int with sign(), we must use a cast to a Double value. No Int-accepting overload exists.
// A negative number has a sign of minus.
let number = -1.0
let result = number.sign
// 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
Odd, even. 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.