2020-12-27 13:43:55 +01:00
|
|
|
#+begin_src elisp tangle: no :results 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
|
|
|
|
|
|
|
|
This type should be used for all charges in the program (electrons, nuclei,...).
|
|
|
|
|
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 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
2020-12-27 13:43:55 +01:00
|
|
|
type t = float
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
** Conversions
|
|
|
|
|
|
|
|
*** ~of_float~ / ~to_float~
|
|
|
|
|
2020-12-27 15:46:11 +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
|
|
|
|
#+end_src
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
2020-12-27 13:43:55 +01:00
|
|
|
external of_float : float -> t = "%identity"
|
|
|
|
external to_float : t -> float = "%identity"
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
*** ~of_int~ / ~to_int~
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval mli)
|
2020-12-27 13:43:55 +01:00
|
|
|
val of_int : int -> t
|
|
|
|
val to_int : t -> int
|
|
|
|
#+end_src
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
2020-12-27 13:43:55 +01:00
|
|
|
let of_int = float_of_int
|
|
|
|
let to_int = int_of_float
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
*** ~of_string~ / ~to_string~
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval mli)
|
2020-12-27 13:43:55 +01:00
|
|
|
val of_string: string -> t
|
|
|
|
val to_string: t -> string
|
|
|
|
#+end_src
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
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"
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
** 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
|
|
|
|
#+end_src
|
|
|
|
|
2020-12-27 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
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 ( /. )
|
|
|
|
#+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 15:46:11 +01:00
|
|
|
#+begin_src ocaml :tangle (eval ml)
|
2020-12-27 13:43:55 +01:00
|
|
|
let pp ppf x =
|
|
|
|
Format.fprintf ppf "@[+%s@]" (to_string x)
|
|
|
|
#+end_src
|
|
|
|
|