10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-18 20:12:26 +01:00
QCaml/common/charge.org

96 lines
2.0 KiB
Org Mode
Raw Normal View History

2020-12-27 16:36:25 +01:00
#+begin_src elisp tangle: no :results none :exports none
2020-12-27 15:46:11 +01:00
(setq pwd (file-name-directory buffer-file-name))
(setq name (file-name-nondirectory (substring buffer-file-name 0 -4)))
(setq lib (concat pwd "lib/"))
(setq testdir (concat pwd "test/"))
(setq mli (concat lib name ".mli"))
(setq ml (concat lib name ".ml"))
(setq test-ml (concat testdir name ".ml"))
2020-12-27 13:43:55 +01:00
(org-babel-tangle)
#+end_src
2020-12-27 15:46:11 +01:00
2020-12-27 13:43:55 +01:00
* Charge
:PROPERTIES:
:header-args: :noweb yes :comments both
:END:
** Type
2020-12-27 15:46:11 +01:00
#+begin_src ocaml :tangle (eval mli)
2020-12-27 13:43:55 +01:00
type t
#+end_src
2020-12-27 17:38:04 +01:00
This type should be used for all charges in the program (electrons, nuclei,...).
2020-12-27 16:36:25 +01:00
#+begin_src ocaml :tangle (eval ml) :exports none
2020-12-27 13:43:55 +01:00
type t = float
#+end_src
** Conversions
2020-12-27 23:08:12 +01:00
#+begin_src ocaml :tangle (eval mli)
2020-12-27 13:43:55 +01:00
val of_float : float -> t
val to_float : t -> float
val of_int : int -> t
val to_int : t -> int
val of_string: string -> t
val to_string: t -> string
2020-12-27 23:08:12 +01:00
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
external of_float : float -> t = "%identity"
external to_float : t -> float = "%identity"
let of_int = float_of_int
let to_int = int_of_float
2020-12-27 13:43:55 +01:00
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"
2020-12-27 23:08:12 +01:00
#+end_src
2020-12-27 13:43:55 +01:00
** Simple operations
2020-12-27 15:46:11 +01:00
#+begin_src ocaml :tangle (eval mli)
2020-12-27 13:43:55 +01:00
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( * ) : t -> float -> t
val ( / ) : t -> float -> t
2021-01-01 11:46:11 +01:00
val is_null : t -> bool
2020-12-27 13:43:55 +01:00
#+end_src
2020-12-27 16:36:25 +01:00
#+begin_src ocaml :tangle (eval ml) :exports none
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
#+end_src
** Printers
2020-12-27 15:46:11 +01:00
#+begin_src ocaml :tangle (eval mli)
2020-12-27 13:43:55 +01:00
val pp : Format.formatter -> t -> unit
#+end_src
2020-12-27 16:36:25 +01:00
#+begin_src ocaml :tangle (eval ml) :exports none
2020-12-27 13:43:55 +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
#+end_src