Skip to content

Commit

Permalink
add unsigned numbers wrap around
Browse files Browse the repository at this point in the history
  • Loading branch information
Jobhdez committed Mar 4, 2024
1 parent d03d8cc commit a4a252c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions library/math/num.lisp
Expand Up @@ -191,15 +191,27 @@

(define (- a b)
(lisp ,type (a b)
(cl:values (cl:mod (cl:- a b) ,(cl:expt 2 bits)))))
(cl:if (cl:< a b)
(cl:ecase ,bits
(8 255)
(16 65535)
(32 4294967295)
(64 18446744073709551615))
(cl:values (cl:mod (cl:- a b) ,(cl:expt 2 bits))))))

(define (* a b)
(lisp ,type (a b)
(cl:values (cl:mod (cl:* a b) ,(cl:expt 2 bits)))))

(define (fromInt x)
(lisp ,type (x)
(cl:values (cl:mod x ,(cl:expt 2 bits))))))))
(cl:if (cl:minusp x)
(cl:ecase ,bits
(8 255)
(16 65535)
(32 4294967295)
(64 18446744073709551615))
(cl:values (cl:mod x ,(cl:expt 2 bits)))))))))

(coalton-toplevel
(define-num-checked Integer cl:identity)
Expand Down

0 comments on commit a4a252c

Please sign in to comment.