# 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.

Web page: https://pseudolearn.app/en/spec/expresiones/potencia

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

## 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.

### Program: Integer and real powers

```pseudolearn
Algorithm Potencias
  Write 2 ^ 10, " ", 2 ^ 0.5, " ", 0 ^ 0, " ", 2.0 ^ 3;
  Write 2 ^ 3 ^ 2, " ", -2 ^ 2;
EndAlgorithm
```

**Output**

```text
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 to the right**

The expression 2 ^ 3 ^ 2 with an arc under 3 ^ 2 showing it is computed first. Below, the result 2 ^ 9 = 512.

Power groups from right to left. First 3 ^ 2 is computed and then 2 ^ 9.

> **Limit**
>
> - With two integers, the exponent cannot be negative: `negativeIntegerExponent`. Use a real for that: `2.0 ^ -1` is 0.5.
> - Any number to the power 0 is 1, including 0 ^ 0.
> - An integer result that does not fit produces `integerOverflow`; an infinite real one, `nonFiniteRealResult`.

> **Common mistakes**
>
> ### Program: Negative exponent between integers
>
> ```pseudolearn
> Algorithm ExponenteNegativo
>   Write 2 ^ -1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `negativeIntegerExponent` (error, 2:11): Integer power does not allow a negative exponent: -1
>
> *The program finishes with an error.*

**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.

**See also**
- [4.7 Precedence and associativity](https://pseudolearn.app/en/spec/expresiones/precedencia.md)
- [12.4 Mathematics and rounding](https://pseudolearn.app/en/spec/biblioteca/matematicas.md)
