on b93c19b help sign in
104 lines 1.3 kB history raw
1+++
2title = "2g: Unsigned Powers"
3weight = 10
4template = "doc.html"
5+++
6### `++pow`
8Computes `a` raised to the power of `b`, producing an atom.
10#### Accepts
12`a` is an atom.
14`b` is an atom.
16#### Produces
18An atom.
20#### Source
22```
23 ++ pow
24 ~/ %pow
25 |= [a=@ b=@]
26 ?: =(b 0) 1
27 |- ?: =(b 1) a
28 =+ c=$(b (div b 2))
29 =+ d=(mul c c)
30 ?~ (dis b 1) d (mul d a)
31```
33#### Examples
35```
36 > (pow 2 6)
37 64
39 > (pow 6 2)
40 36
42 > (pow 7 (add 2 2))
43 2.401
45 > (pow 2 0)
46 1
48 > `@ux`(pow 0x1b 2)
49 0x2d9
50```
52---
53### `++sqt`
55Computes the square root of `a` and its remainder.
57#### Accepts
59`a` is an atom.
61#### Produces
63A cell of atoms.
65#### Source
67```
68 ++ sqt
69 ~/ %sqt
70 |= a=@ ^- [p=@ q=@]
71 ?~ a [0 0]
72 =+ [q=(div (dec (xeb a)) 2) r=0]
73 =- [-.b (sub a +.b)]
74 ^= b |-
75 =+ s=(add r (bex q))
76 =+ t=(mul s s)
77 ?: =(q 0)
78 ?:((lte t a) [s t] [r (mul r r)])
79 ?: (lte t a)
80 $(r s, q (dec q))
81 $(q (dec q))
82```
84#### Examples
86```
87 > (sqt 4)
88 [p=2 q=0]
90 > (sqt 6)
91 [p=2 q=2]
93 > (sqt 2)
94 [p=1 q=1]
96 > (sqt 0b1101)
97 [p=3 q=4]
99 > `@ux`(sqt 0b1101)
100 ! exit
101```
104---