This commit is contained in:
swrup 2026-02-11 00:14:15 +01:00
parent a7d61ef43d
commit 25f7b9682b

View file

@ -2,7 +2,6 @@
have a currency agnostic amount_lib.ml have a currency agnostic amount_lib.ml
and specialize amount.ml to Config.currency?? and specialize amount.ml to Config.currency??
no Zarith
have safe amount arithmetic *) have safe amount arithmetic *)
type sign = type sign =
| Sign_plus | Sign_plus
@ -15,6 +14,7 @@ type t = {
fraction: Int32.t; fraction: Int32.t;
} }
(* rm Zarith *)
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
(* TODO (* TODO
@ -24,14 +24,12 @@ let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
let fraction_upper_bound = Int32.of_int 100_000_000 let fraction_upper_bound = Int32.of_int 100_000_000
let make ~sign ~currency ~value ~fraction = let make ~sign ~currency ~value ~fraction =
let ( let* ) o f = match o with _e, false -> f () | e, true -> Error e in if value < Int64.zero then Error "value is negative"
let* () = ("value is negative", value < Int64.zero) in else if fraction < Int32.zero then Error "fraction is negative"
let* () = ("fraction is negative", fraction < Int32.zero) in else if value > value_upper_bound then Error "value is greater than 2^52-1"
let* () = ("value is greater than 2^52-1", value > value_upper_bound) in else if fraction >= fraction_upper_bound then
let* () = Error "fraction has more than 8 decimal digits"
("fraction has more than 8 decimal digits", fraction >= fraction_upper_bound) else Ok { sign; currency; value; fraction }
in
Ok { sign; currency; value; fraction }
module Parse = struct module Parse = struct
open Angstrom open Angstrom