1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Computes `x` to the power of `n` using exponentiation by squaring.
///
/// The algorithm is adapted from
/// <https://en.wikipedia.org/w/index.php?title=Exponentiation_by_squaring&oldid=1229001691#With_constant_auxiliary_memory>,
/// removing the negative case:
///
/// ```text
/// Function exp_by_squaring_iterative(x, n)
/// if n < 0 then
/// x := 1 / x;
/// n := -n;
/// if n = 0 then return 1
/// y := 1;
/// while n > 1 do
/// if n is odd then
/// y := x * y;
/// n := n - 1;
/// x := x * x;
/// n := n / 2;
/// return x * y
/// ```
///
/// On IEEE 754 compliant systems, this always gives the same results given the
/// same inputs.