mirror of
https://gitlab.com/scemama/QCaml.git
synced 2024-11-19 04:22:21 +01:00
43 lines
1.2 KiB
OCaml
43 lines
1.2 KiB
OCaml
(* [[file:../charge.org::*Type][Type:2]] *)
|
|
type t = float
|
|
(* Type:2 ends here *)
|
|
|
|
(* [[file:../charge.org::*~of_float~ / ~to_float~][~of_float~ / ~to_float~:2]] *)
|
|
external of_float : float -> t = "%identity"
|
|
external to_float : t -> float = "%identity"
|
|
(* ~of_float~ / ~to_float~:2 ends here *)
|
|
|
|
(* [[file:../charge.org::*~of_int~ / ~to_int~][~of_int~ / ~to_int~:2]] *)
|
|
let of_int = float_of_int
|
|
let to_int = int_of_float
|
|
(* ~of_int~ / ~to_int~:2 ends here *)
|
|
|
|
(* [[file:../charge.org::*~of_string~ / ~to_string~][~of_string~ / ~to_string~:2]] *)
|
|
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"
|
|
(* ~of_string~ / ~to_string~:2 ends here *)
|
|
|
|
(* [[file:../charge.org::*Simple operations][Simple operations:2]] *)
|
|
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 ( /. )
|
|
(* Simple operations:2 ends here *)
|
|
|
|
(* [[file:../charge.org::*Printers][Printers:2]] *)
|
|
let pp ppf x =
|
|
Format.fprintf ppf "@[+%s@]" (to_string x)
|
|
(* Printers:2 ends here *)
|