on master help sign in
Merge pull request #308 from urbit/rmariani-patch-30

Update 3a.md

commit8c4a0354707dd8d85e8309fef8f47226ef1bc420 authorJoshua Reagan <joshuareagan@users.noreply.github.com> dateFri 24 Aug 2018, 11:09 (8 years ago) parent95710a7cc7af72
1 file changed+443−614
docs/hoon/library/3a.md+443−614
docs/hoon/library/3a.md+443−614
@@ -2,756 +2,633 @@
22navhome: /docs/
33---
44
5+# 3a: modular and signed ints
56
6−# 3a: signed and modular ints
7−### `++fe` :: modulo bloq
7+### `++egcd`
88
9−Modulo bloq
9+Extended Euclidean algorithm
1010
11−Core containing XX
11+Produces `d`, the
12+[greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
13+of `a` and `b`. Also produces `u` and `v` such that `au + bv = GCD(a, b)`.
1214
13−Accepts
14−-------
15+#### Accepts
1516
16−`a` is a `++bloq`.
17+`a` is an atom.
1718
18−Source
19−------
19+`b` is an atom.
2020
21− |_ a=bloq
21+#### Produces
2222
23−### `++dif`
23+`d`, the greatest common divisor, is an atom.
2424
25−Produces the difference between two atoms in the modular basis
26−representation.
25+`u`, the coefficient of `a`, is a signed integer.
2726
28−Accepts
29−-------
27+`v`, the coefficient of `b`, is a signed integer.
3028
31−`a` is a `++bloq`.
29+#### Source
3230
33−`b` is an atom.
31+ ++ egcd
32+ |= [a=@ b=@]
33+ =+ si
34+ =+ [c=(sun a) d=(sun b)]
35+ =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
36+ |- ^- [d=@ u=@s v=@s]
37+ ?: =(--0 c)
38+ [(abs d) d.u d.v]
39+ =+ q=(fra d c)
40+ %= $
41+ c (dif d (pro q c))
42+ d c
43+ u [(dif d.u (pro q c.u)) c.u]
44+ v [(dif d.v (pro q c.v)) c.v]
45+ ==
3446
35−`c` is an atom.
3647
37−Produces
38−--------
48+#### Examples
3949
40−A `@s`
50+ > (egcd 11 2)
51+ [d=1 u=--1 v=-5]
4152
42−Source
43−------
53+***
54+### `++fo`
4455
45− ++ dif |=([b/@ c/@] (sit (sub (add out (sit b)) (sit c))))
56+Modulo prime
4657
47−Examples
48−--------
58+Container core for modular arithmetic functions.
4959
50− > (~(dif fe 3) 63 64)
51− 255
52− > (~(dif fe 3) 5 10)
53− 251
54− > (~(dif fe 3) 0 1)
55− 255
56− > (~(dif fe 0) 9 10)
57− 1
58− > (~(dif fe 0) 9 11)
59− 0
60− > (~(dif fe 0) 9 12)
61− 1
62− > (~(dif fe 2) 9 12)
63− 13
64− > (~(dif fe 2) 63 64)
65− 15
60+#### Source
6661
67−------------------------------------------------------------------------
62+ ++ fo
63+ ^?
64+ |_ a=@
6865
69−### `++inv`
7066
71−Invert mod field
67+#### `++dif`
68+
69+Subtraction
70+
71+Produces the difference between atoms `b` and `c`, with `a` as the modular base.
7272
73−Inverts the order of the modular field.
73+#### Accepts
7474
75−Accepts
76−-------
75+`a` is an atom.
7776
78−`b` is a `++bloq`.
77+`b` is an atom.
78+
79+`c` is an atom.
7980
80−Produces
81−--------
81+#### Produces
8282
8383An atom.
8484
85+#### Source
8586
86−Source
87−------
87+ ++ dif
88+ |= [b=@ c=@]
89+ (sit (sub (add a b) (sit c)))
8890
89− ++ inv |=(b/@ (sub (dec out) (sit b)))
91+#### Examples
9092
91−Examples
92−--------
93+ > (~(dif fo 6) 1 2)
94+ 5
9395
94− > (~(inv fe 3) 255)
95− 0
96− > (~(inv fe 3) 256)
97− 255
98− > (~(inv fe 3) 0)
99− 255
100− > (~(inv fe 3) 1)
101− 254
102− > (~(inv fe 3) 2)
103− 253
104− > (~(inv fe 3) 55)
105− 200
96+ > (~(dif fo 21) 11 45)
97+ 8
98+
99+#### `++exp`
106100
107−------------------------------------------------------------------------
101+Exponent
108102
109−### `++net`
103+Produces the power of `b` raised to the `c`, with `a` as the modular base.
110104
111−Reverse bytes
105+#### Accepts
112106
113−Revereses bytes within block.
107+`a` is an atom.
114108
115−Accepts
116−-------
109+`b` is an atom.
117110
118−`b` is a `++bloq`.
111+`c` is an atom.
119112
120−Produces
121−--------
113+#### Produces
122114
123115An atom.
124116
125−Source
126−------
127−
128− ++ net |= b/@ ^- @
129− => .(b (sit b))
130− ?: (lte a 3)
131− b
132− =+ c=(dec a)
133− %+ con
134− (lsh c 1 $(a c, b (cut c [0 1] b)))
135− $(a c, b (cut c [1 1] b))
136−
137−Examples
138−--------
139−
140− > (~(net fe 3) 64)
141− 64
142− > (~(net fe 3) 128)
143− 128
144− > (~(net fe 3) 255)
145− 255
146− > (~(net fe 3) 256)
147− 0
148− > (~(net fe 3) 257)
149− 1
150− > (~(net fe 3) 500)
151− 244
152− > (~(net fe 3) 511)
153− 255
154− > (~(net fe 3) 512)
155− 0
156− > (~(net fe 3) 513)
157− 1
158− > (~(net fe 3) 0)
159− 0
160− > (~(net fe 3) 1)
161− 1
162− > (~(net fe 0) 1)
163− 1
164− > (~(net fe 0) 2)
165− 0
166− > (~(net fe 0) 3)
117+#### Source
118+
119+ ++ exp
120+ |= [b=@ c=@]
121+ ?: =(0 b)
122+ 1
123+ =+ d=$(b (rsh 0 1 b))
124+ =+ e=(pro d d)
125+ ?:(=(0 (end 0 1 b)) e (pro c e))
126+
127+#### Examples
128+
129+ > (~(exp fo 5) 8 2)
167130 1
168− > (~(net fe 6) 1)
169− 72.057.594.037.927.936
170− > (~(net fe 6) 2)
171− 144.115.188.075.855.872
172− > (~(net fe 6) 3)
173− 216.172.782.113.783.808
174− > (~(net fe 6) 4)
175− 288.230.376.151.711.744
176− > (~(net fe 6) 5)
177− 360.287.970.189.639.680
178− > (~(net fe 6) 6)
179− 432.345.564.227.567.616
180− > (~(net fe 6) 7)
181− 504.403.158.265.495.552
182− > (~(net fe 6) 512)
183− 562.949.953.421.312
184− > (~(net fe 6) 513)
185− 72.620.543.991.349.248
186131
187−------------------------------------------------------------------------
132+ > (~(exp fo 95) 8 2)
133+ 66
134+
135+ > (~(exp fo 195) 8 2)
136+ 61
137+
138+ > (~(exp fo 995) 8 2)
139+ 256
140+
141+***
142+### `++fra`
188143
189−### `++out`
144+Divide
190145
191−Max integer value
146+Produces the quotient of `b` divided by `c`, with `a` as the modular base.
192147
193−The maximum integer value that the current block can store.
148+#### Accepts
194149
195−Accepts
196−-------
150+`a` is an atom.
197151
198−A `++bloq`.
152+`b` is an atom.
199153
200−Produces
201−--------
154+`c` is an atom.
155+
156+#### Produces
202157
203158An atom.
204159
205−Source
206−------
160+#### Accepts
207161
208− ++ out (bex (bex a))
162+`a` is an atom.
209163
210−Examples
211−--------
164+`b` is an atom.
212165
213− > ~(out fe 0)
214− 2
215− > ~(out fe 1)
216− 4
217− > ~(out fe 2)
218− 16
219− > ~(out fe 3)
220− 256
221− > ~(out fe 4)
222− 65.536
223− > ~(out fe 10)
224− \/179.769.313.486.231.590.772.930.519.078.902.473.361.797.697.894.230.657.\/
225− 273.430.081.157.732.675.805.500.963.132.708.477.322.407.536.021.120.113.
226− 879.871.393.357.658.789.768.814.416.622.492.847.430.639.474.124.377.767.
227− 893.424.865.485.276.302.219.601.246.094.119.453.082.952.085.005.768.838.
228− 150.682.342.462.881.473.913.110.540.827.237.163.350.510.684.586.298.239.
229− 947.245.938.479.716.304.835.356.329.624.224.137.216
230− \/ \/
166+#### Source
231167
232−------------------------------------------------------------------------
168+ ++ fra
169+ |= [b=@ c=@]
170+ (pro b (inv c))
233171
234−### `++rol`
172+#### Examples
235173
236−Roll left
174+ > (~(fra fo 2) 8 2)
175+ 0
237176
238−Roll `d` to the left by `c` `b`-sized blocks.
177+ > (~(fra fo 3) 8 2)
178+ 1
239179
240−Accepts
241−-------
180+ > (~(fra fo 4) 8 2)
181+ 0
242182
243−`a` is a `++bloq`.
183+ > (~(fra fo 5) 8 2)
184+ 4
244185
245−`b` is a `++bloq`.
186+***
187+### `++inv`
246188
247−`c` is an atom.
189+Inverse
248190
249−`d` is an atom.
191+Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
192+`u` is produced by taking the `++egcd` of `a` and `b`.
250193
251−Produces
252−--------
194+#### Accepts
195+
196+`a` is an atom.
197+
198+`b` is an atom.
199+
200+#### Produces
253201
254202An atom.
255203
256−Source
257−------
204+#### Source
205+
206+ ++ inv
207+ |= b=@
208+ =+ c=(dul:si u:(egcd b a) a)
209+ c
210+
211+#### Examples
258212
259− ++ rol |= [b/bloq c/@ d/@] ^- @ :: roll left
260− =+ e=(sit d)
261− =+ f=(bex (sub a b))
262− =+ g=(mod c f)
263− (sit (con (lsh b g e) (rsh b (sub f g) e)))
213+ > (~(inv fo 11) 2)
214+ 6
264215
265−Examples
266−--------
216+ > (~(inv fo 71) 255)
217+ 22
267218
268− > `@ux`(~(rol fe 6) 4 3 0xabac.dedf.1213)
269− 0x1213.0000.abac.dedf
270− > `@ux`(~(rol fe 6) 4 2 0xabac.dedf.1213)
271− 0xdedf.1213.0000.abac
272− > `@t`(~(rol fe 5) 3 1 'dfgh')
273− 'hdfg'
274− > `@t`(~(rol fe 5) 3 2 'dfgh')
275− 'ghdf'
276− > `@t`(~(rol fe 5) 3 0 'dfgh')
277− 'dfgh'
219+ > (~(inv fo 79) 255)
220+ 22
278221
279−------------------------------------------------------------------------
222+ > (~(inv fo 78) 255)
223+ 67
280224
281−### `++ror`
225+ > (~(inv fo 70) 255)
226+ 67
282227
283−Roll right
228+***
229+### `++pro`
284230
285−Roll `d` to the right by `c` `b`-sized blocks.
231+Multiplication
286232
287−Accepts
288−-------
233+Produces the multiplication of `b` and `c` modulo `a`.
289234
290−`a` is a `++bloq`.
235+#### Accepts
291236
292−`b` is a `++bloq`.
237+`a` is an atom.
293238
294−`c` is an atom.
239+`b` is an atom.
295240
296−`d` is an atom.
241+`c` is an atom.
297242
298−Produces
299−--------
243+#### Produces
300244
301245An atom.
302246
303−Source
304−------
305−
306− ++ ror |= {b/bloq c/@ d/@} ^- @ :: roll right
307− =+ e=(sit d)
308− =+ f=(bex (sub a b))
309− =+ g=(mod c f)
310− (sit (con (rsh b g e) (lsh b (sub f g) e)))
247+#### Source
311248
249+ ++ pro
250+ |= [b=@ c=@]
251+ (sit (mul b c))
312252
313−Examples
314−--------
253+#### Examples
315254
316− > `@ux`(~(ror fe 6) 4 1 0xabac.dedf.1213)
317− 0x1213.0000.abac.dedf
318− > `@ux`(~(ror fe 6) 3 5 0xabac.dedf.1213)
319− 0xacde.df12.1300.00ab
320− > `@ux`(~(ror fe 6) 3 3 0xabac.dedf.1213)
321− 0xdf12.1300.00ab.acde
322− > `@t`(~(rol fe 5) 3 0 'hijk')
323− 'hijk'
324− > `@t`(~(rol fe 5) 3 1 'hijk')
325− 'khij'
326− > `@t`(~(rol fe 5) 3 2 'hijk')
327− 'jkhi'
255+ > (~(pro fo 3) 11 4)
256+ 2
328257
329−------------------------------------------------------------------------
258+ > (mod 44 3)
259+ 2
330260
331−### `++sum`
261+***
262+### `++sit`
332263
333−Sum
264+Modulus
334265
335−Sum two numbers in this modular field.
266+Produces the remainder of `b` modulo `a`.
336267
337−Accepts
338−-------
268+#### Accepts
339269
340−`a` is a `++bloq`.
270+`a` is an atom.
341271
342272`b` is an atom.
343273
344−`c` is an atom.
345−
346−Produces
347−--------
274+#### Produces
348275
349276An atom.
350277
351−Source
352−------
278+#### Source
353279
354− ++ sum |=([b/@ c/@] (sit (add b c))) :: wrapping add
280+ ++ sit
281+ |= b=@
282+ (mod b a)
355283
356−Examples
357−--------
284+#### Examples
358285
359− > (~(sum fe 3) 10 250)
360− 4
361− > (~(sum fe 0) 0 1)
362− 1
363− > (~(sum fe 0) 0 2)
364− 0
365− > (~(sum fe 2) 14 2)
366− 0
367− > (~(sum fe 2) 14 3)
368− 1
369− > (~(sum fe 4) 10.000 256)
370− 10.256
371− > (~(sum fe 4) 10.000 100.000)
372− 44.464
286+ > (~(sit fo 3) 14)
287+ 2
373288
374−### `++sit`
289+***
290+### `++sum`
375291
376−Atom in mod block representation
292+Modular sum
377293
378−Produce an atom in the current modular block representation.
294+Produces the remainder of `(b + c) mod a`.
379295
380−Accepts
381−-------
296+#### Accepts
382297
383−`a` is a `++bloq`.
298+`a` is an atom.
384299
385300`b` is an atom.
386301
387−Produces
388−--------
302+`c` is an atom.
303+
304+#### Produces
389305
390306An atom.
391307
392−Source
393−------
308+#### Source
394309
395− ++ sit |=(b/@ (end a 1 b)) :: enforce modulo
310+ ++ sum
311+ |= [b=@ c=@]
312+ (sit (add b c))
313+ --
396314
397−Examples
398−--------
399−
400− > (~(sit fe 3) 255)
401− 255
402− > (~(sit fe 3) 256)
403− 0
404− > (~(sit fe 3) 257)
405− 1
406− > (~(sit fe 2) 257)
407− 1
408− > (~(sit fe 2) 10.000)
409− 0
410− > (~(sit fe 2) 100)
411− 4
412− > (~(sit fe 2) 19)
413− 3
414− > (~(sit fe 2) 17)
415− 1
416− > (~(sit fe 0) 17)
417− 1
418− > (~(sit fe 0) 0)
419− 0
420− > (~(sit fe 0) 1)
421− 1
315+#### Examples
422316
317+ > (~(sum fo 3) 14 3)
318+ 2
423319
320+ > (mod 17 3)
321+ 2
424322
425323***
426324### `++si`
427325
428− ++ si !: :: signed integer
326+Signed integer
327+
328+Container core for signed integer functions.
329+
330+#### Source
331+
332+ ++ si
333+ ^?
429334 |%
430335
431−Container core for the signed integer functions.
336+#### Discussion
337+
338+The signed-integer type is represented by the `@s` aura. Positive integers are
339+represented with a two prepending `-`s, and negative integers are represented
340+with two; `--1` and `-1`, respectively.
432341
433−------------------------------------------------------------------------
342+Positive signed integers correspond to odd atoms of twice their absolute value,
343+and negative signed integers correspond to atoms of twice their absolute value
344+minus one. For example:
434345
435−### `++abs`
346+ > `@`--4
347+ 8
348+ > `@s`8
349+ --4
350+
351+ > `@`-4
352+ 7
353+ > `@s`7
354+ -4
355+
356+***
357+### `++abs:si`
436358
437359Absolute value
438360
439−Produces the absolute value of a signed integer.
361+Produces the absolute value of signed integer `a`.
440362
441−Accepts
442−-------
363+#### Accepts
443364
444−`a` is a signed integer, `@s`.
365+`a` is a signed integer.
445366
446−Produces
447−--------
367+#### Produces
448368
449369An atom.
450370
451−Source
452−------
453−
454− ++ abs |=(a/@s (add (end 0 1 a) (rsh 0 1 a))) :: absolute value
371+### Source
455372
456−Examples
457−--------
373+ ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
458374
459− > (abs:si -2)
460− 2
461− > (abs:si -10.000)
462− 10.000
463− > (abs:si --2)
464− 2
375+#### Examples
465376
466−------------------------------------------------------------------------
377+ > (abs:si -11)
378+ 11
467379
468−### `++dif`
380+ > (abs:si --520)
381+ 520
469382
470−Difference
383+***
384+### `++dif:si`
471385
472−Produces the difference between two signed integers `b` and `c`.
386+Subtraction
473387
474−Accepts
475−-------
388+Produces the difference of `a` minus `b`.
476389
477−`a` is a signed integer, `@s`.
390+#### Accepts
478391
479−`b` is a signed integer, `@s`.
392+`a` is a signed integer.
480393
481−Produces
482−--------
394+`b` is a signed integer.
483395
484−A `@s`.
396+#### Produces
485397
398+A signed integer.
486399
487−Source
488−------
400+#### Source
489401
490− ++ dif |= [a/@s b/@s] :: subtraction
402+ ++ dif |= [a=@s b=@s]
491403 (sum a (new !(syn b) (abs b)))
492404
493−Examples
494−--------
405+#### Examples
495406
496− > (dif:si --10 -7)
497− --17
498− > (dif:si --10 --7)
499− --3
500− > (dif:si `@s`0 --7)
501− -7
502− > (dif:si `@s`0 `@s`7)
503− --4
407+ > (dif:si --3 -2)
408+ --5
504409
505−------------------------------------------------------------------------
410+ > (dif:si -3 --2)
411+ -5
506412
507−### `++dul`
413+***
414+### `++dul:si`
508415
509416Modulus
510417
511−Produces the modulus of two signed integers.
418+Produces the remainder of `b` modulo `a`.
512419
513−Accepts
514−-------
420+#### Examples
515421
516−`a` is a signed integer, `@s`.
422+`a` is a signed integer.
517423
518424`b` is an atom.
519425
520−Produces
521−--------
426+#### Produces
522427
523−An atom.
428+An atom.
524429
525−Source
526−------
430+#### Source
527431
528− ++ dul |= [a/@s b/@] :: modulus
432+ ++ dul |= [a=@s b=@]
529433 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
530434
531−Examples
532−--------
435+#### Examples
533436
534− > (dul:si --9 3)
535− 0
536− > (dul:si --9 4)
537− 1
538− > (dul:si --9 5)
539− 4
540− > (dul:si --9 6)
541− 3
542− > (dul:si --90 --10)
437+ > `@s`(dul:si -1 --5)
438+ -5
439+
440+ > `@`--5
543441 10
442+ > `@s`(dul:si -1 10)
443+ -5
544444
545−------------------------------------------------------------------------
445+ > `@s`(dul:si -11 -61)
446+ --55
546447
547−### `++fra`
448+
449+***
450+#### `++fra:si`
548451
549452Divide
550453
551−Produces the quotient of two signed integers.
454+Produces the quotient of `b` divided by `c`.
552455
553−Accepts
554−-------
456+#### Accepts
555457
556−`a` is a signed integer, `@s`.
458+`a` is a signed integer.
557459
558−`b` is a signed integer, `@s`.
460+`b` is a signed integer.
559461
560−Produces
561−--------
462+#### Produces
562463
563−An atom.
464+A signed atom.
564465
565−Source
566−------
466+#### Source
567467
568− ++ fra |= [a/@s b/@s] :: divide
468+ ++ fra |= [a=@s b=@s]
569469 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
570470
571−Examples
572−--------
471+#### Examples
573472
574− > (fra:si --10 -2)
473+ > (fra:si -1 -1)
474+ --1
475+
476+ > (fra:si -11 --2)
575477 -5
576− > (fra:si -20 -5)
577− --4
578478
579−------------------------------------------------------------------------
479+ > (fra:si -0 -1)
480+ --0
481+
580482
483+***
581484### `++new`
582485
583−Signed integer
486+Atom to @s
584487
585−Produces a signed integer from a loobean sign value `a` and an atom `b`.
488+Produces a signed integer from an atom `b`. The product's sign is determined
489+by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
490+result in a prepending `-`.
586491
587−Accepts
588−-------
492+#### Accepts
589493
590−`a` is a loobean.
494+`a` is a flag.
591495
592496`b` is an atom.
593497
594−Produces
595−--------
498+#### Produces
596499
597−A `@s`
500+A signed integer.
598501
599−Source
600−------
502+#### Source
601503
602− ++ new |= [a/? b/@] :: [sign value] to @s
504+ ++ new |= [a=? b=@]
603505 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
604506
605−Examples
606−--------
507+#### Examples
607508
608− > (new:si [& 10])
609− --10
610− > (new:si [| 10])
611− -10
612− > (new:si [%.y 7])
613− --7
509+ > (new:si | 2)
510+ -2
614511
615−------------------------------------------------------------------------
512+ > (new:si & 2)
513+ --2
616514
617−### `++old`
515+ > (new:si & -2)
516+ --3
618517
619−sign value
518+ > (new:si & --2)
519+ --4
620520
621−Produces the cell `[sign value]` representations of a signed integer.
521+***
522+### `++old:si`
622523
623−Accepts
624−-------
524+Sign and absolute value
625525
626−`a` is a `@s`.
526+Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
527+positive or negative, and the absolute value of `a`.
627528
628−Produces
629−--------
529+#### Accepts
630530
631−A cell of a boolean and an atom.
531+`a` is a signed integer.
632532
633−Source
634−------
533+#### Produces
635534
636− ++ old |=(a/@s [(syn a) (abs a)]) :: [sign value]
535+A cell composed of a `term` and an atom.
637536
638−Examples
639−--------
537+#### Source
640538
641− > (old:si 7)
642− ! type-fail
643− ! exit
644− > (old:si -7)
645− [%.n 7]
646− > (old:si --7)
647− [%.y 7]
648− > (old:si `@s`7)
649− [%.n 4]
650− > (old:si -0)
651− [%.y 0]
539+ ++ old |=(a=@s [(syn a) (abs a)])
652540
653−------------------------------------------------------------------------
541+#### Examples
654542
655−### `++pro`
543+ > (old:si -2)
544+ [%.n 2]
656545
657−Multiply
546+ > (old:si --2)
547+ [%.y 2]
658548
659−Produces the product of two signed integers.
549+***
550+### `++pro`
660551
661−Accepts
662−-------
552+Multiplication
663553
664−`a` is a `@s`.
554+Produces a signed integer by multiplying `a` and `b`.
665555
666−`b` is a `@s`.
556+#### Accepts
667557
668−Produces
669−--------
558+`a` is an unsigned integer.
670559
671−A `@s`.
560+`b` is an unsigned integer.
672561
673−Source
674−------
562+#### Source
675563
676− ++ pro |= [a/@s b/@s] :: multiplication
564+ ++ pro |= [a=@s b=@s]
677565 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
678566
679−Examples
680−--------
567+#### Examples
681568
682− > (pro:si -4 --2)
683− -8
684− > (pro:si -4 -2)
685− --8
686− > (pro:si --10.000.000 -10)
687− -100.000.000
688− > (pro:si -1.337 --0)
689− --0
569+ > (pro:si -3 -3)
570+ --9
690571
691−------------------------------------------------------------------------
572+ > (pro:si -3 --3)
573+ -9
692574
693−### `++rem`
575+***
576+### `++rem:si`
694577
695578Remainder
696579
697−Produces the remainder from a division of two signed integers.
580+Produces a signed integer that is the remainder of `a` divided by `b`.
698581
699−Accepts
700−-------
582+#### Accepts
701583
702−`a` is a `@s`.
584+`a` is a signed integer.
703585
704−`b` is a `@s`.
586+`b` is a signed integer.
705587
706−Produces
707−--------
588+#### Produces
708589
709−A `@sd`.
590+A signed integer.
710591
711−Source
712−------
592+#### Source
713593
714− ++ rem |=([a/@s b/@s] (dif a (pro b (fra a b)))) :: remainder
594+ ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
715595
716−Examples
717−--------
596+#### Examples
718597
719− > (rem:si -10 -4)
598+ > (rem:si -17 -3)
720599 -2
721− > (rem:si --10 --4)
600+
601+ > (rem:si --17 -3)
722602 --2
723− > (rem:si --10 -4)
603+
604+ > (rem:si -17 --3)
605+ -2
606+
607+ > (rem:si --17 --3)
724608 --2
725− > (rem:si --7 --3)
726− --1
727− > (rem:si --0 --10.000)
728− --0
729609
730−------------------------------------------------------------------------
731610
732−### `++sum`
733611
734−Add
612+***
613+### `++sum:si`
614+
615+Addition
735616
736−Sum two signed integers.
617+Produces an atom by adding `a` and `b`.
737618
738−Accepts
739−-------
619+#### Accepts
740620
741−`b` is a `@s`.
621+`a` is a signed integer.
742622
743−`c` is a signed integer `@s`.
623+`b` is a signed integer.
744624
745−Produces
746−--------
625+#### Produces
747626
748−A `@s`.
627+A signed integer.
749628
750−Source
751−------
629+#### Source
752630
753− ++ sum |= [a/@s b/@s] :: addition
754− ~| %si-sum
631+ ++ sum |= [a=@s b=@s]
755632 =+ [c=(old a) d=(old b)]
756633 ?: -.c
757634 ?: -.d
@@ -765,115 +642,104 @@ Source
765642 (new & (sub +.d +.c))
766643 (new | (add +.c +.d))
767644
768−Examples
769−--------
645+#### Examples
770646
771− > (sum:si --10 --10)
772− --20
773− > (sum:si --10 -0)
774− --10
775− > (sum:si -10 -7)
776− -17
777− > (sum:si -10 --7)
778− -3
647+ > (sum:si -11 --2)
648+ -9
779649
780−------------------------------------------------------------------------
650+ > (sum:si --2 --2)
651+ --4
781652
782−### `++sun`
653+***
654+#### `++sun:si`
783655
784−Signed from unsigned
656+@u to @s
785657
786−Produces a signed integer from an unsigned integer.
658+Multiplies the unsigned integer `a` by two, producing an atom.
787659
788−Note that the result must be manually cast to some `@s` odor to be
789−inferred as an unsigned integer in the type system.
660+#### Accepts
790661
791−Accepts
792−-------
662+`a` is an unsigned integer.
793663
794−`a` is a `@u`.
664+#### Produces
795665
796−Produces
797−--------
666+An atom.
798667
799−A `@s`.
668+#### Source
800669
801−Source
802−------
670+ ++ sun |=(a=@u (mul 2 a))
803671
804− ++ sun |=(a/@u (mul 2 a)) :: @u to @s
672+#### Examples
805673
806−Examples
807−--------
674+ > (mul 2 90)
675+ 180
808676
809− > `@s`10
810− --5
811− > (sun:si 10)
812− 20
813− > `@s`(sun:si 10)
814− --10
815− > `@sd`(sun:si 10)
816− --10
817− > `@sd`(sun:si 12.345)
818− --12.345
677+ > (mul 2 --90)
678+ 360
679+ > `@u`--90
680+ 180
819681
820−------------------------------------------------------------------------
682+ > (mul 2 --89)
683+ 356
684+ > `@u`--89
685+ 178
821686
822−### `++syn`
687+ > (mul 2 -89)
688+ 354
689+ > `@u`-89
690+ 177
823691
824−Sign
692+***
693+### `++syn:si`
694+
695+Sign test
825696
826−Produce the sign of a signed integer, where `&` is posiitve, and `|` is negative.
697+Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
698+is positive, and `%.n` is produced if `a` is negative.
827699
828−Accepts
829−-------
700+#### Accepts
830701
831−`a` is a `@s`.
702+`a` is a signed integer.
832703
833−Produces
834−--------
704+#### Produces
835705
836−A boolean.
706+An `term`.
837707
838−Source
839−------
708+#### Source
840709
841− ++ syn |=(a/@s =(0 (end 0 1 a))) :: sign test
710+ ++ syn |=(a=@s =(0 (end 0 1 a)))
842711
843−Examples
844−--------
712+#### Examples
845713
846714 > (syn:si -2)
847715 %.n
716+
848717 > (syn:si --2)
849718 %.y
850− > (syn:si -0)
851− %.y
852719
853−------------------------------------------------------------------------
854720
855−### `++cmp`
721+***
722+### `++cmp:si`
856723
857724Compare
858725
859−Compare two signed integers.
726+Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
727+is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
728+equal, `--0` is produced.
860729
861−Accepts
862−-------
730+#### Accepts
863731
864−`b` is a `@s`.
732+`a` is a signed integer.
865733
866−`c` is a `@s`.
734+`b` is a signed integer.
867735
868−Produces
869−--------
736+#### Produces
870737
871−A `@s`.
738+A signed integer.
872739
873−Source
874−------
740+#### Source
875741
876− ++ cmp |= [a/@s b/@s] :: compare
742+ ++ cmp |= [a=@s b=@s]
877743 ^- @s
878744 ?: =(a b)
879745 --0
@@ -883,65 +749,28 @@ Source
883749 --1
884750 -1
885751 --1
886− ?: (syn b)
887− -1
888− ?: (gth a b)
889− -1
890− --1
752+ ?: (syn b)
753+ -1
754+ ?: (gth a b)
755+ -1
756+ --1
757+ --
891758
892−Examples
893−--------
759+#### Examples
894760
895− > (cmp:si --10 --10)
896− --0
897− > (cmp:si --10 -0)
898− --1
899− > (cmp:si -10 -7)
900− -1
901− > (cmp:si -10 --7)
761+ > (cmp:si -2 --1)
902762 -1
903763
764+ > (cmp:si -2 --1)
765+ -1
904766
905−***
906−### `++egcd`
907−
908−GCD
909−
910− ++ egcd !: :: schneier's egcd
911− |= {a/@ b/@}
912− =+ si
913− =+ [c=(sun a) d=(sun b)]
914− =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
915− |- ^- {d/@ u/@s v/@s}
916− ?: =(--0 c)
917− [(abs d) d.u d.v]
918− :: ?> ?& =(c (sum (pro (sun a) c.u) (pro (sun b) c.v)))
919− :: =(d (sum (pro (sun a) d.u) (pro (sun b) d.v)))
920− :: ==
921− =+ q=(fra d c)
922− %= $
923− c (dif d (pro q c))
924− d c
925− u [(dif d.u (pro q c.u)) c.u]
926− v [(dif d.v (pro q c.v)) c.v]
927− ==
928− ::
929−
930−
931−Greatest common denominator
932−
933− > (egcd 20 15)
934− [d=5 u=2 v=1]
935− > (egcd 24 16)
936− [d=8 u=2 v=1]
937− > (egcd 7 5)
938− [d=1 u=3 v=6]
939− > (egcd (shaf ~ %ham) (shaf ~ %sam))
940− [ d=1
941− u=59.983.396.314.566.203.239.184.568.129.921.874.787
942− v=38.716.650.351.034.402.960.165.718.823.532.275.722
943− ]
767+ > (cmp:si --2 --1)
768+ --1
944769
770+ > (cmp:si --2 --2)
771+ --0
945772
773+ > (cmp:si --2 --5)
774+ -1
946775
947776***