10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-19 12:32:21 +01:00
QCaml/common/lib/charge.ml

44 lines
997 B
OCaml
Raw Normal View History

2020-12-27 23:08:12 +01:00
(* This type should be used for all charges in the program (electrons, nuclei,...). *)
2020-12-27 13:43:55 +01:00
(* [[file:../charge.org::*Type][Type:2]] *)
2018-01-02 22:02:01 +01:00
type t = float
2020-12-27 13:43:55 +01:00
(* Type:2 ends here *)
2018-01-02 22:02:01 +01:00
2020-12-27 23:08:12 +01:00
(* [[file:../charge.org::*Conversions][Conversions:2]] *)
2018-03-16 00:23:47 +01:00
external of_float : float -> t = "%identity"
external to_float : t -> float = "%identity"
2020-12-27 13:43:55 +01:00
let of_int = float_of_int
let to_int = int_of_float
let of_string = float_of_string
2018-01-02 22:02:01 +01:00
let to_string x =
2020-12-27 13:43:55 +01:00
if x > 0. then
2018-01-02 22:02:01 +01:00
Printf.sprintf "+%f" x
2020-12-27 13:43:55 +01:00
else if x < 0. then
2018-01-02 22:02:01 +01:00
Printf.sprintf "%f" x
2020-12-27 13:43:55 +01:00
else
"0.0"
2020-12-27 23:08:12 +01:00
(* Conversions:2 ends here *)
2020-12-27 13:43:55 +01:00
(* [[file:../charge.org::*Simple operations][Simple operations:2]] *)
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 ( /. )
(* Simple operations:2 ends here *)
(* [[file:../charge.org::*Printers][Printers:2]] *)
2018-03-16 00:23:47 +01:00
let pp ppf x =
2020-12-27 13:43:55 +01:00
Format.fprintf ppf "@[+%s@]" (to_string x)
(* Printers:2 ends here *)