Swift 中的幂运算符

在 Swift 语言的参考文献中,我没有看到在基本算术运算符中定义的指数运算符。

语言中真的没有预定义的整数或浮点运算符吗?

76437 次浏览

Like most of the C-family of languages, there isn't one.

There isn't one but you have the pow function.

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

I did it like so:

operator infix ** { associativity left precedence 200 }


func ** (base: Double, power: Double) -> Double {
return exp(log(base) * power)
}

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.

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

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
}

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

Swift 4.2

import Foundation


var n = 2.0 // decimal
var result = 5 * pow(n, 2)
print(result)
// 20.0

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)