on 4fdc19d help sign in
799 lines 6.9 kB history raw
1+++
2title = "1a: Basic Arithmetic"
3weight = 1
4template = "doc.html"
5+++
7## `++add`
9Add
11Produces the sum of `a` and `b`.
13#### Accepts
15`a` is an [`atom`](/docs/glossary/atom).
17`b` is an [`atom`](/docs/glossary/atom).
19#### Produces
21An [`atom`](/docs/glossary/atom).
23#### Source
25```hoon
26++ add
27 ~/ %add
28 |= [a=@ b=@]
29 ^- @
30 ?: =(0 a) b
31 $(a (dec a), b +(b))
32```
34#### Examples
36```
37> (add 2 2)
39```
41```
42> (add 1 1.000.000)
431.000.001
44```
46```
47> (add 1.333 (mul 2 2))
481.337
49```
51```
52> (add 'a' 4)
53101
54```
56```
57> (add 'a' 'b')
58195
59```
61```
62> (add 0xc 0xf)
6327
64```
66```
67> `@ux`(add 0xc 0xf)
680x1b
69```
71---
73## `++dec`
75Decrement
77Decrements `a` by `1`.
79#### Accepts
81`a` is an [`atom`](/docs/glossary/atom).
83#### Produces
85An [`atom`](/docs/glossary/atom).
87#### Source
89```hoon
90++ dec
91 ~/ %dec
92 |= a=@
93 ~_ leaf+"decrement-underflow"
94 ?< =(0 a)
95 =+ b=0
96 |- ^- @
97 ?: =(a +(b)) b
98 $(b +(b))
99```
101#### Examples
103```
104> (dec 7)
106```
108```
109> (dec 0)
110! decrement-underflow
111! exit
112```
114```
115> (dec 'b')
11697
117> `@t`(dec 'b')
118'a'
119```
121```
122> (dec 0xc)
12311
124> `@ux`(dec 0xc)
1250xb
126```
128---
130## `++div`
132Divide
134Computes `a` divided by `b` without remainder.
136#### Accepts
138`a` is an [`atom`](/docs/glossary/atom).
140`b` is an [`atom`](/docs/glossary/atom).
142#### Produces
144An [`atom`](/docs/glossary/atom).
146#### Source
148```hoon
149++ div
150 ~/ %div
151 |: [a=`@`1 b=`@`1]
152 ^- @
153 ~_ leaf+"divide-by-zero"
154 ?< =(0 b)
155 =+ c=0
156 |-
157 ?: (lth a b) c
158 $(a (sub a b), c +(c))
159```
161#### Examples
163```
164> (div 4 2)
166```
168```
169> (div 17 8)
171```
173```
174> (div 20 30)
176```
178```
179> (div 'ab' 'a')
180259
181> `@`'ab'
18225.185
183> `@`a
18497
185```
187```
188> (div 0x9f 0x29)
190```
192---
194## `++dvr`
196Divide with remainder
198Computes `a` divided by `b`, producing the quotient and the remainder.
200#### Accepts
202`a` is an [`atom`](/docs/glossary/atom).
204`b` is an [`atom`](/docs/glossary/atom).
206#### Produces
208A cell of [`atoms`](/docs/glossary/atom).
210#### Source
212```hoon
213++ dvr
214 ~/ %dvr
215 |= [a=@ b=@]
216 ^- [p=@ q=@]
217 [(div a b) (mod a b)]
218```
220#### Examples
222```
223> (dvr 17 8)
224[p=2 q=1]
225```
227```
228> (dvr 17 6)
229[p=2 q=5]
230```
232```
233> (dvr 18 6)
234[p=3 q=0]
235```
237```
238> (dvr 4 2)
239[p=2 q=0]
240```
242```
243> (dvr 'ab 'a')
244[p=259 q=62]
245> `@`'a'
24697
247> `@`'ab'
24825.185
249```
251---
253## `++gte`
255Greater-than/equal
257Tests whether `a` is greater than or equal to `b`.
259#### Accepts
261`a` is an [`atom`](/docs/glossary/atom).
263`b` is an [`atom`](/docs/glossary/atom).
265#### Produces
267A flag.
269#### Source
271```hoon
272++ gte
273 ~/ %gte
274 |= [a=@ b=@]
275 ^- ?
276 !(lth a b)
277```
279#### Examples
281```
282> (gte 100 10)
283%.y
284```
286```
287> (gte 4 4)
288%.y
289```
291```
292> (gte 3 4)
293%.n
294```
296---
298## `++gth`
300Greater-than
302Tests whether `a` is greater than `b`.
304#### Accepts
306`a` is an [`atom`](/docs/glossary/atom).
308`b` is an [`atom`](/docs/glossary/atom).
310#### Produces
312A flag.
314#### Source
316```hoon
317++ gth
318 ~/ %gth
319 |= [a=@ b=@]
320 ^- ?
321 !(lte a b)
322```
324#### Examples
326```
327> (gth 'd' 'c')
328%.y
329```
331```
332> (gth ~h1 ~m61)
333%.n
334```
336```
337> (gth 'steak' 'brisket')
338%.n
339```
341---
343## `++lte`
345Less-than/equal
347Tests whether `a` is less than or equal to `b`.
349#### Accepts
351`a` is an [`atom`](/docs/glossary/atom).
353`b` is an [`atom`](/docs/glossary/atom).
355#### Produces
357A flag.
359#### Source
361```hoon
362++ lte
363 ~/ %lte
364 |= [a=@ b=@]
365 |(=(a b) (lth a b))
366```
368#### Examples
370```
371> (lte 4 5)
372%.y
373```
375```
376> (lte 5 4)
377%.n
378```
380```
381> (lte 5 5)
382%.y
383```
385```
386> (lte 0 0)
387%.y
388```
390```
391> (lte 'a' 'b')
392%.y
393> `@`'a'
39497
395> `@`'b'
39698
397```
399```
400> (lte 'abraham' 'bob')
401%.n
402```
404```
405> (lte ~m61 ~h1)
406%.n
407```
409---
411## `++lth`
413Less-than
415Tests whether `a` is less than `b`.
417#### Accepts
419`a` is an [`atom`](/docs/glossary/atom).
421`b` is an [`atom`](/docs/glossary/atom).
423#### Produces
425A flag.
427#### Source
429```hoon
430++ lth
431 ~/ %lth
432 |= [a=@ b=@]
433 ^- ?
434 ?& !=(a b)
435 |-
436 ?| =(0 a)
437 ?& !=(0 b)
438 $(a (dec a), b (dec b))
439 == == ==
440```
442#### Examples
444```
445> (lth 4 5)
446%.y
447```
449```
450> (lth 5 4)
451%.n
452```
454```
455> (lth 5 5)
456%.n
457```
459```
460> (lth 5 0)
461%.n
462```
464```
465> (lth 'a' 'b')
466%.y
467> `@`'a'
46897
469> `@`'b'
47098
471```
473```
474> (lth 'abraham' 'bob')
475%.n
476```
478---
480## `++max`
482Maximum
484Computes the greater of `a` and `b`.
486#### Accepts
488`a` is an [`atom`](/docs/glossary/atom).
490`b` is an [`atom`](/docs/glossary/atom).
492#### Produces
494An [`atom`](/docs/glossary/atom).
496#### Source
498```hoon
499++ max
500 ~/ %max
501 |= [a=@ b=@]
502 ^- @
503 ?: (gth a b) a
504 b
505```
507#### Examples
509```
510> (max 10 100)
511100
512```
514```
515> (max 10.443 9)
51610.443
517```
519```
520> (max 1 1)
522```
524```
525> (max 'abraham' 'bob')
52630.787.873.400.840.801
527```
529```
530> `@t`(max 'abraham' 'bob')
531'abraham'
532```
534```
535> `@t`(max 'abraham' 'benjamin')
536'benjamin'
537```
539```
540> `@dr`(max ~m6 ~h1)
541~h1
542```
544```
545>`@ux`(max 0xe1 0x1e)
5460xe1
547```
549---
551## `++min`
553Minimum
555Computes the lesser of `a` and `b`.
557#### Accepts
559`a` is an [`atom`](/docs/glossary/atom).
561`b` is an [`atom`](/docs/glossary/atom).
563#### Produces
565An [`atom`](/docs/glossary/atom).
567#### Source
569```hoon
570++ min
571 ~/ %min
572 |= [a=@ b=@]
573 ^- @
574 ?: (lth a b) a
575 b
576```
578#### Examples
580```
581> (min 10 100)
58210
583```
585```
586> (min 10.443 9)
588```
590```
591> (min 1 1)
593```
595```
596> (min 'abraham' 'bob')
5976.451.042
598```
600```
601> `@t`(min 'abraham' 'bob')
602'bob'
603```
605```
606> `@t`(min 'abraham' 'benjamin')
607'abraham'
608```
610```
611> `@dr`(min ~m6 ~h1)
612~m6
613```
615---
617## `++mod`
619Modulus
621Computes the remainder of dividing `a` by `b`.
623#### Accepts
625`a` is an [`atom`](/docs/glossary/atom).
627`b` is an [`atom`](/docs/glossary/atom).
629#### Produces
631An [`atom`](/docs/glossary/atom).
633#### Source
635```hoon
636++ mod
637 ~/ %mod
638 |: [a=`@`1 b=`@`1]
639 ^- @
640 ?< =(0 b)
641 (sub a (mul b (div a b)))
642```
644#### Examples
646```
647> (mod 5 2)
649```
651```
652> (mod 5 5)
654```
656```
657> (mod 2 5)
659```
661```
662> (mod 40 20)
664```
666```
667> `@`'c'
66899
669>`@`'a'
67097
671> (mod 'c' 'a')
673```
675---
677## `++mul`
679Multiply
681Multiplies `a` by `b`.
683#### Accepts
685`a` is an [`atom`](/docs/glossary/atom).
687`b` is an [`atom`](/docs/glossary/atom).
689#### Produces
691An [`atom`](/docs/glossary/atom).
693#### Source
695```hoon
696++ mul
697 ~/ %mul
698 |: [a=`@`1 b=`@`1]
699 ^- @
700 =+ c=0
701 |-
702 ?: =(0 a) c
703 $(a (dec a), c (add b c))
704```
706#### Examples
708```
709> (mul 3 4)
71012
711```
713```
714> (mul 0 1)
716```
718```
719> `@`'a'
72097
721> `@`'b'
72298
723> (mul 'a' 'b')
7249.506
725```
727```
728>`@ux`(mul 0x1f 0xe)
7290x1b2
730```
732---
734## `++sub`
736Subtract
738Subtracts `b` from `a`.
740#### Accepts
742`a` is an [`atom`](/docs/glossary/atom).
744`b` is an [`atom`](/docs/glossary/atom).
746#### Produces
748An [`atom`](/docs/glossary/atom).
750#### Source
752```hoon
753++ sub
754 ~/ %sub
755 |= [a=@ b=@]
756 ~_ leaf+"subtract-underflow"
757 ^- @
758 ?: =(0 b) a
759 $(a (dec a), b (dec b))
760```
762#### Examples
764```
765> (sub 10 5)
767```
769```
770> (sub 243 44)
771199
772```
774```
775> (sub 5 0)
777```
779```
780> (sub 0 5)
781! subtract-underflow
782! exit
783```
785```
786> (sub 'c' 'a')
788> `@`'c'
78999
790> `@`'a'
79197
792```
794```
795> (sub 0x1f 0xe)
79617
797> `@ux`(sub 0x1f 0xe)
7980x11
799```