This commit is contained in:
swrup 2025-10-18 20:49:43 +02:00
parent 90c404468b
commit ebab6c900e

View file

@ -19,22 +19,19 @@ type t = {
} }
let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
let check_not msg = function false -> Ok () | true -> Error msg
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
let fraction_upper_bound = Int64.of_int 100_000_000
let make ~sign ~currency ~value ~fraction = let make ~sign ~currency ~value ~fraction =
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in let* () = check_not "value is negative" (value < Int64.zero) in
let fraction_upper_bound = Int64.of_int 100_000_000 in let* () = check_not "fraction is negative" (fraction < Int64.zero) in
let* () = if value < Int64.zero then Error "value is negative" else Ok () in
let* () = let* () =
if fraction < Int64.zero then Error "fraction is negative" else Ok () check_not "value is greater than 2^52" (value > value_upper_bound)
in in
let* () = let* () =
if value > value_upper_bound then Error "value is greater than 2^52" check_not "fraction has more than 8 decimal digits"
else Ok () (fraction >= fraction_upper_bound)
in
let* () =
if fraction >= fraction_upper_bound then
Error "fraction have more than 8 decimal digits"
else Ok ()
in in
Ok { sign; currency; value; fraction } Ok { sign; currency; value; fraction }