Update 2g
Wrote documentation for this previously undocumented section
commit5e328dc1dc4288bbcc05cdcf5e452240818fbd9e
authorrmariani <robert@tlon.io>
dateTue 5 Jun 2018, 11:54 (8 years ago)
parente8793ab
docs/hoon/library/2g.md+94−1
@@ -3,4 +3,97 @@ navhome: /docs/
33---
44
55
6−# 2g: unsigned powers (currently undocumented)
6+# 2g: unsigned powers
7+### `++pow`
8+
9+Computes `a` raised to the power of `b`, producing an atom.
10+
11+#### Accepts
12+
13+`a` is an atom.
14+
15+`b` is an atom.
16+
17+#### Produces
18+
19+An atom.
20+
21+#### 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+
32+#### Examples
33+
34+ > (pow 2 6)
35+ 64
36+
37+ > (pow 6 2)
38+ 36
39+
40+ > (pow 7 (add 2 2))
41+ 2.401
42+
43+ > (pow 2 0)
44+ 1
45+
46+ > `@ux`(pow 0x1b 2)
47+ 0x2d9
48+
49+
50+
51+***
52+### `++sqt`
53+
54+Computes the square root of `a` and its remainder.
55+
56+#### Accepts
57+
58+`a` is an atom.
59+
60+#### Produces
61+
62+A cell of atoms.
63+
64+#### Source
65+ ++ sqt
66+ ~/ %sqt
67+ |= a=@ ^- [p=@ q=@]
68+ ?~ a [0 0]
69+ =+ [q=(div (dec (xeb a)) 2) r=0]
70+ =- [-.b (sub a +.b)]
71+ ^= b |-
72+ =+ s=(add r (bex q))
73+ =+ t=(mul s s)
74+ ?: =(q 0)
75+ ?:((lte t a) [s t] [r (mul r r)])
76+ ?: (lte t a)
77+ $(r s, q (dec q))
78+ $(q (dec q))
79+
80+#### Examples
81+
82+ > (sqt 4)
83+ [p=2 q=0]
84+
85+ > (sqt 6)
86+ [p=2 q=2]
87+
88+ > (sqt 2)
89+ [p=1 q=1]
90+
91+ > (sqt 0b1101)
92+ [p=3 q=4]
93+
94+ > `@ux`(sqt 0b1101)
95+ ! exit
96+
97+
98+
99+***