I don't see an exponentiation operator defined in the base arithmetic operators in the Swift language reference.
Is there really no predefined integer or float exponentiation operator in the language?
I don't see an exponentiation operator defined in the base arithmetic operators in the Swift language reference.
Is there really no predefined integer or float exponentiation operator in the language?
There isn't an operator but you can use the pow function like this:
return pow(num, power)
If you want to, you could also make an operator call the pow function like this:
infix operator ** { associativity left precedence 170 }
func ** (num: Double, power: Double) -> Double{
return pow(num, power)
}
2.0**2.0 //4.0
**
, so you can use it on ints and not conflict with XOR. –
Menken -2.0**2.0 = -(2.0**2.0) = -4.0
. However, here -2.0**2.0 = (-2.0)**2.0 = 4.0
, which might not be the intended use and could causes quite a nasty and hard to track down bug. –
Arlinearlington <<
and >>
. Different precedences will lead to different code interpretations, so standardizing on a precedence for common operators is important. I don't know what the best standard is, but giving << 2
and ** 2
the same precedence makes some sense. nshipster.com/swift-operators –
Calciferol infix operator ** : BitwiseShiftPrecedence
–
Frumentaceous If you happen to be raising 2 to some power, you can use the bitwise left shift operator:
let x = 2 << 0 // 2
let y = 2 << 1 // 4
let z = 2 << 7 // 256
Notice that the 'power' value is 1 less than you might think.
Note that this is faster than pow(2.0, 8.0)
and lets you avoid having to use doubles.
let x = 0b00000001 << exponent // 2**exponent let x = 1 << 0 // 1 let x = 1 << 2 // 4 let x = 1 << 8 // 256
–
Enwreathe 1 << exponent
, because you’re literally moving the bit left on a small endian value. 1 << 1 == 2
, or imagine 00000001 gets the bit shifted left 1, giving 00000010, which is 2 in binary. I like this solution though. I incorporated it into my comprehensive answer below. –
Neper For anyone looking for a Swift 3 version of the **
infix operator:
precedencegroup ExponentiationPrecedence {
associativity: right
higherThan: MultiplicationPrecedence
}
infix operator ** : ExponentiationPrecedence
func ** (_ base: Double, _ exp: Double) -> Double {
return pow(base, exp)
}
func ** (_ base: Float, _ exp: Float) -> Float {
return pow(base, exp)
}
2.0 ** 3.0 ** 2.0 // 512
(2.0 ** 3.0) ** 2.0 // 64
import Darwin
to get pow
–
Sandusky 2**3**2
is 512, not 64. I don't know any programming language with a left-associative exponentiation operator. They're all right-associative. If you implement this in Swift, you should definitely do it right-associative to be consistent with other popular languages, as well as mathematical convention. –
Fried Swift 4.2
import Foundation
var n = 2.0 // decimal
var result = 5 * pow(n, 2)
print(result)
// 20.0
I did it like so:
operator infix ** { associativity left precedence 200 }
func ** (base: Double, power: Double) -> Double {
return exp(log(base) * power)
}
Like most of the C-family of languages, there isn't one.
If you're specifically interested in the exponentiation operator for Int
type, I don't think that existing answers would work particularly well for large numbers due to the way how floating point numbers are represented in memory. When converting to Float
or Double
from Int
and then back (which is required by pow
, powf
and powl
functions in Darwin
module) you may lose precision. Here's a precise version for Int
:
let pow = { Array(repeating: $0, count: $1).reduce(1, *) }
Note that this version isn't particularly memory efficient and is optimized for source code size.
Another version that won't create an intermediate array:
func pow(_ x: Int, _ y: Int) -> Int {
var result = 1
for i in 0..<y {
result *= x
}
return result
}
-1**-1 = -1
, -10**-1 = -0.1
. –
Neper pow(Double, Double)
result as an Int. I’ve provided a new answer with that solution, but also optimizing for speed by handling boundary cases and using the bitwise left shift operator for powers of 2 and -2. –
Neper An alternative answer is to use NSExpression
let mathExpression = NSExpression(format:"2.5**2.5")
let answer = mathExpression.expressionValue(with: nil, context: nil) as? Double
or
let mathExpression = NSExpression(format:"2**3")
let answer = mathExpression.expressionValue(with: nil, context: nil) as? Int
This answer provides a tested and optimized* function for calculating integer powers of integers, while also providing several versions of the custom **
operators for exponentiation.
* At least I think it’s optimized, based on what I read on this page.
My guess is that Swift deliberately does not provide this because of the need to choose what to do about results that have absolute values of less than 1. Do you want it to round to 0 or implicitly cast to a decimal type? The compiler can’t know, and choosing a default may lead to people using it without realizing what mathematical choice they just made.
In Swift 5.3:
import Foundation
precedencegroup ExponeniationPrecedence {
associativity: right // This makes Towers of Powers work correctly
higherThan: MultiplicationPrecedence
}
infix operator ** : ExponeniationPrecedence
public func **(_ base: Int, _ exponent: Int) -> Int {
return pow(base, exponent)
}
public func **(_ base: Double, _ exponent: Double) -> Double {
return pow(base, exponent)
}
public func **(_ base: Decimal, _ exponent: Int) -> Decimal {
return pow(base, exponent)
}
public func **(_ base: Float, _ exponent: Float) -> Float {
return powf(base, exponent)
}
/// Calculate exponentiation of integer base and integer exponent, returning integer result.
///
/// Exponentiation that would result in absolute values of less than 1 (i.e. exponent is negative and base is not 1 or -1) are rounded 0.
public func pow(_ base: Int, _ exponent: Int) -> Int {
// Optimize cases for certain exponents
switch exponent {
case 0:
return 1
case 1:
return base
case _ where exponent < 0 && base != -1 && base != 1:
// Negative exponents of integers always round to zero, except if the base is 1 or -1
return 0
default:
break
}
// Optimize cases for certain bases
switch base {
case -1:
if exponent % 2 == 0 {
return -1 * base
} else {
return base
}
case 0, 1:
return base
case -2, 2:
// Use the bitwise left shift operator to efficiently calculate powers of 2 and -2
let result = 1 << exponent
if base == -2 && exponent % 2 == 1 {
return -1 * result
}
return result
default:
var result = 1
for i in 1 ... exponent {
result *= base
}
return result
}
}
/// Calculate powers of integer base and integer exponent using Foundation's pow function by casting both the base and the exponent as Doubles, calling pow, but without casting the result.
/// Useful if rounding results between -1 and 1 to zero is not acceptable.
public func pow(_ base: Int, _ exponent: Int) -> Double {
return pow(Double(base), Double(exponent))
}
/// Calculate powers of integer base and integer exponent using Foundation's pow function by casting both the base and the exponent as Doubles, calling pow, and then casting the result as an Int
/// If results are -1<x<1, round to 0.
public func castPow(_ base: Int, _ exponent: Int) -> Int {
return Int(pow(base, exponent))
}
Test cases for the pow(Int, Int)
function:
// Test Exponent = 0
assert(0**0 == 1)
assert(1**0 == 1)
assert(2**0 == 1)
// Test Exponent = 1
assert(-1**1 == -1)
assert(0**1 == 0)
assert(1**1 == 1)
assert(2**1 == 2)
// Test Exponent = -1
assert(-1 ** -1 == -1)
assert(0 ** -1 == 0)
assert(1 ** -1 == 1)
assert(2 ** -1 == 0)
// Test Exponent = 2
assert(-1 ** 2 == 1)
assert(0 ** 2 == 0)
assert(1 ** 2 == 1)
assert(2 ** 2 == 4)
assert(3 ** 2 == 9)
// Test Base = 0
assert(0**0 == 1)
assert(0**1 == 0)
assert(0**2 == 0)
// Test Base = 1
assert(1 ** -1 == 1)
assert(1**0 == 1)
assert(1**1 == 1)
assert(1**2 == 1)
// Test Base = -1
assert(-1 ** -1 == -1)
assert(-1**0 == 1)
assert(-1**1 == -1)
assert(-1**2 == 1)
assert(-1**2 == 1)
assert(-1**3 == -1)
// Test Base = 2
assert(2 ** -1 == 0)
assert(2**0 == 1)
assert(2**1 == 2)
assert(2**2 == 4)
assert(2**3 == 8)
// Test Base = -2
assert(-2 ** -1 == 0)
assert(-2**0 == 1)
assert(-2**1 == -2)
assert(-2**2 == 4)
assert(-2**3 == -8)
// Test Base = 3
assert(3 ** -1 == 0)
assert(3**0 == 1)
assert(3**1 == 3)
assert(3**2 == 9)
assert(3**3 == 27)
// Test Towers of Powers
assert(2**2**2 == 16)
assert(3**2**2 == 81)
assert(2**2**3 == 256)
assert(2**3**2 == 512)
© 2022 - 2024 — McMap. All rights reserved.