10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-06 22:23:42 +01:00
QCaml/common/charge.org
2020-12-27 16:36:25 +01:00

107 lines
2.2 KiB
Org Mode

#+begin_src elisp tangle: no :results none :exports none
(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"))
(org-babel-tangle)
#+end_src
* Charge
:PROPERTIES:
:header-args: :noweb yes :comments both
:END:
** Type
This type should be used for all charges in the program (electrons, nuclei,...).
#+begin_src ocaml :tangle (eval mli)
type t
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
type t = float
#+end_src
** Conversions
*** ~of_float~ / ~to_float~
#+begin_src ocaml :tangle (eval mli)
val of_float : float -> t
val to_float : t -> float
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
external of_float : float -> t = "%identity"
external to_float : t -> float = "%identity"
#+end_src
*** ~of_int~ / ~to_int~
#+begin_src ocaml :tangle (eval mli)
val of_int : int -> t
val to_int : t -> int
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
let of_int = float_of_int
let to_int = int_of_float
#+end_src
*** ~of_string~ / ~to_string~
#+begin_src ocaml :tangle (eval mli)
val of_string: string -> t
val to_string: t -> string
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
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
#+begin_src ocaml :tangle (eval mli)
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( * ) : t -> float -> t
val ( / ) : t -> float -> t
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
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
#+begin_src ocaml :tangle (eval mli)
val pp : Format.formatter -> t -> unit
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
let pp ppf x =
Format.fprintf ppf "@[+%s@]" (to_string x)
#+end_src