Skip to content

SpecificationIV · Expressions and operators

4.3

Power

^ raises one number to another: 2 ^ 10 is 1024; between integers the result is exact, and with a real it is real.

Markdown version

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;
EndAlgorithm

Output

1024 1.4142135623730951 1 8.0
512 4

The program finishes normally.

Two details worth knowing:

  • Power associates to the right: 2 ^ 3 ^ 2 is 2 ^ (3 ^ 2), that is, 2 to the 9th.
  • The sign binds more than the power: -2 ^ 2 is (-2) ^ 2, which is 4.
Figure 4.3-1.Power groups from right to left. First 3 ^ 2 is computed and then 2 ^ 9.

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 ^ b is the power; between integers it is exact.
  • It associates to the right: 2 ^ 3 ^ 2 is 512.
  • The sign applies first: -2 ^ 2 is 4.
  • Between integers the exponent cannot be negative.