Swift Math: abs, sqrt and powUse math methods. Call abs, max, min, sqrt, floor, ceil and pow.
Math. Algorithms often change numbers in specific ways. An array index is always positive. 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.
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.
Swift program that uses abs
let number = -10
// Use abs to make number positive.
let result = abs(number)
// An absolute value is always positive.
let result2 = abs(result)
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.
Max: This returns the larger of two numbers. It handles negative numbers. Here it returns 2000 because that was the larger argument.
Min: Returns the smaller of two numbers. In this example we get 10 because 2000 is larger than 10.
Swift program that uses max, min
let small = 10
let big = 2000
// Compute max.
let resultMax = max(small, big)
// Compute min.
let resultMin = min(small, big)
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.
Swift program that uses sqrt method
// 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.
Swift program that uses floor method
// 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.
Swift program that uses ceil method
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.
Swift program that uses pow method
// 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.
Tip: To test an Int with sign(), we must use a cast to a Double value. No Int-accepting overload exists.
Swift program that uses signbit
// 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
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.Odd, Even
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.Fibonacci
A review. 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.