Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exponentiation operator in Swift

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?

like image 809
mcgregor94086 Avatar asked Jun 05 '14 16:06

mcgregor94086


People also ask

What is an exponentiation operator?

The exponentiation operator ( ** ) returns the result of raising the first operand to the power of the second operand. It is equivalent to Math. pow , except it also accepts BigInts as operands.

What is the operator in Swift?

An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator ( + ) adds two numbers, as in let i = 1 + 2 , and the logical AND operator ( && ) combines two Boolean values, as in if enteredDoorCode && passedRetinaScan .

What is exponentiation in programming?

In mathematics and computer programming, exponentiating by squaring is a general method for fast computation of large positive integer powers of a number, or more generally of an element of a semigroup, like a polynomial or a square matrix.


Video Answer


1 Answers

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 
like image 200
Connor Avatar answered Sep 21 '22 00:09

Connor