10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-23 04:43:32 +01:00
QCaml/common/lib/charge.ml
2024-01-17 10:30:24 +01:00

37 lines
733 B
OCaml

(* This type should be used for all charges in the program (electrons, nuclei,...). *)
type t = float
(** Conversions *)
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"
(** Simple operations *)
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.
(** Printers *)
let pp ppf x =
Format.fprintf ppf "@[%s@]" (to_string x)