QCaml/common/lib/charge.ml

33 lines
590 B
OCaml

type t = float
external of_float : float -> t = "%identity"
external to_float : t -> float = "%identity"
let of_int = float_of_int
let to_int = int_of_float
let of_string = float_of_string
let to_string x =
if x > 0. then
Printf.sprintf "+%f" x
else if x < 0. then
Printf.sprintf "%f" x
else
"0.0"
let gen_op op =
fun a b ->
op (to_float a) (to_float b)
|> of_float
let ( + ) = gen_op ( +. )
let ( - ) = gen_op ( -. )
let ( * ) = gen_op ( *. )
let ( / ) = gen_op ( /. )
let is_null t = t == 0.
let pp ppf x =
Format.fprintf ppf "@[%s@]" (to_string x)