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?
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With