4.3.1 How it works
The power a ^ b multiplies a by itself b times. As with the other operators, the type of the result depends on the operands: two integers give an exact integer; if either is real, the result is real.
ProgramInteger and real powers
Algorithm Potencias
Write 2 ^ 10, " ", 2 ^ 0.5, " ", 0 ^ 0, " ", 2.0 ^ 3;
Write 2 ^ 3 ^ 2, " ", -2 ^ 2;
EndAlgorithmOutput
1024 1.4142135623730951 1 8.0 512 4
The program finishes normally.
Two details worth knowing:
- Power associates to the right:
2 ^ 3 ^ 2is2 ^ (3 ^ 2), that is, 2 to the 9th. - The sign binds more than the power:
-2 ^ 2is(-2) ^ 2, which is 4.
The expression 2 ^ 3 ^ 2 with an arc under 3 ^ 2 showing it is computed first. Below, the result 2 ^ 9 = 512.
In short
a ^ bis the power; between integers it is exact.- It associates to the right:
2 ^ 3 ^ 2is 512. - The sign applies first:
-2 ^ 2is 4. - Between integers the exponent cannot be negative.