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

102 lines
2.1 KiB
Org Mode
Raw Normal View History

2020-12-27 13:43:55 +01:00
#+begin_src elisp tangle: no :results none
(org-babel-tangle)
#+end_src
* Charge
:PROPERTIES:
:ml: lib/charge.ml
:mli: lib/charge.mli
:test-ml: test/charge.ml
: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 (org-entry-get nil "mli" t)
type t
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
type t = float
#+end_src
** Conversions
*** ~of_float~ / ~to_float~
#+begin_src ocaml :tangle (org-entry-get nil "mli" t)
val of_float : float -> t
val to_float : t -> float
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
external of_float : float -> t = "%identity"
external to_float : t -> float = "%identity"
#+end_src
*** ~of_int~ / ~to_int~
#+begin_src ocaml :tangle (org-entry-get nil "mli" t)
val of_int : int -> t
val to_int : t -> int
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
let of_int = float_of_int
let to_int = int_of_float
#+end_src
*** ~of_string~ / ~to_string~
#+begin_src ocaml :tangle (org-entry-get nil "mli" t)
val of_string: string -> t
val to_string: t -> string
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
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 (org-entry-get nil "mli" t)
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( * ) : t -> float -> t
val ( / ) : t -> float -> t
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
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 (org-entry-get nil "mli" t)
val pp : Format.formatter -> t -> unit
#+end_src
#+begin_src ocaml :tangle (org-entry-get nil "ml" t)
let pp ppf x =
Format.fprintf ppf "@[+%s@]" (to_string x)
#+end_src