^ (power) operator

Type Assocation Precedence Input types Output types
binary left 6 float
complex
float
complex

This operator returns a to the bth power in a^b. It can be used to square (^2) or cube (^3) expressions. The type of the result is equal to the largest type of the operands (complex is larger than float). Note that integer operands are automatically converted to float by the compiler, so the result is always a float expression. Examples:

  3 ^ 2            ; 9.0  3.23 ^ 4.9       ; 312.6744  (3, 1) ^ 3       ; (18, 26)  (2, 3) ^ (2, 1)  ; (-4.8379, -0.5170)    

The compiler determines the type of the operator (float or complex) by looking at the context in which it is used. If the type is float, a^b is evaluated as abs(a)^b. If you don’t want this to happen, you must assign the result of the operation to a complex variable, or make sure one of the arguments is a complex value. This is important because the power operator can return complex results with float operands. Examples:

  float a = 2 ^ 3             ; 8  float a = (-2) ^ 3          ; 8  float a = real((-2,0) ^ 3)  ; -8  complex a = (-2) ^ 3        ; (-8, 0)  complex a = 4 + (-2) ^ 3    ; (-4, 0)    

See Also
Expressions
Types

^ (power) operator