Exponentiation operator in Swift

北城余情 提交于 2019-11-28 19:59:59

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
chanceoperation

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
ldanilek

I did it like so:

operator infix ** { associativity left precedence 200 }

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

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

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
}

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

Swift 4.2

var n = 1.0
var result = 45 * pow(n, 3) + 20 * pow(n, 2) + 19
print(result)
// 84.0

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!