I’d like to announce a new library: checked_oint
. It implements checked arithmetic for both signed and unsigned integers of 8, 16, 32, 64, and 128 bits. Unlike stdint
or ocaml-integers
, routines in this library either return an option or raise an exception when a result of an arithmetic operation cannot be represented in a desired integer type. In addition, it contains abstractions for manipulating arbitrary integers and integer types in a generic and type-safe manner, which I find quite useful for compiler/interpreter implementations.
Usage example:
open Checked_oint
let () =
let x = U8.of_int_exn 50 in
let y = U8.of_int_exn 70 in
assert (U8.equal (U8.add_exn x y) (U8.of_int_exn 120));
assert (Option.is_none (U8.mul x y))
Feel free to ask any questions in the comments.