10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-07 06:33:39 +01:00
QCaml/common/lib/charge.ml

46 lines
1.0 KiB
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,...). *)
2021-01-01 16:39:33 +01:00
(* [[file:~/QCaml/common/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
2021-01-01 16:39:33 +01:00
(* [[file:~/QCaml/common/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
2021-01-01 16:39:33 +01:00
(* [[file:~/QCaml/common/charge.org::*Simple%20operations][Simple operations:2]] *)
2020-12-27 13:43:55 +01:00
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 ( /. )
2021-01-01 11:46:11 +01:00
let is_null t = t == 0.
2020-12-27 13:43:55 +01:00
(* Simple operations:2 ends here *)
2021-01-01 16:39:33 +01:00
(* [[file:~/QCaml/common/charge.org::*Printers][Printers:2]] *)
2018-03-16 00:23:47 +01:00
let pp ppf x =
2021-01-01 11:46:11 +01:00
Format.fprintf ppf "@[%s@]" (to_string x)
2020-12-27 13:43:55 +01:00
(* Printers:2 ends here *)