add example of unsigned number vocabulary

FossilOrigin-Name: 94de33e961466ffd42bb28a81dfb974b2f8034555de7e99fd2295641316cb36e
This commit is contained in:
crc 2023-09-20 11:54:52 +00:00
parent b2e807278e
commit 0cb7f0ad99

21
example/unsigned.retro Normal file
View file

@ -0,0 +1,21 @@
This is a vocabulary for doing some basic operations on unsigned
numbers.
~~~
:u:patch (n-n)
dup n:negative?
[ #2147483647 n:add n:inc swap #-2147483648 n:sub n:add ] if ;
:u:add (nn-n) n:add u:patch ;
:u:sub (nn-n) n:sub u:patch ;
:u:mul (nn-n) n:mul u:patch ;
:u:div (nn-n)
dup n:zero? [ drop-pair n:MAX ] [ n:div u:patch ] choose ;
:u:mod (nn-n)
dup n:zero? [ drop-pair n:MAX ] [ n:mod u:patch ] choose ;
:u:eq? (nn-f) or not ;
:u:-eq? (nn-f) u:eq? not ;
:u:lt? (nn-f) over or and n:negative? ;
:u:gt? (nn-f) tuck or and n:negative? ;
~~~