1 (***********************************************************************)
3 (* (community) Objective Caml *)
5 (* Edgar Friendly <thelema314@gmail.com> *)
7 (* Copyright 2008 Edgar Friendly. *)
8 (* All rights reserved. This file is distributed *)
9 (* under the terms of the GNU Library General Public License, with *)
10 (* the special exception on linking described in file ../LICENSE. *)
12 (* Copyright 2008 <bluestorm dot dylc on-the-server gmail dot com> *)
13 (***********************************************************************)
15 module type OrderedType
=
18 val compare
: t
-> t
-> int
21 module type ComparableType
=
24 val compare
: t
-> t
-> int
25 val equal
: t
-> t
-> bool
28 module type PrintableType
=
31 val to_string
: t
-> string
34 module type SerializableType
=
37 val to_string
: t
-> string
38 val of_string
: string -> t
41 module type DiscreteType
= sig
50 val to_int
: t
-> int (* may raise error if not with int range *)
52 val of_string
: string -> t
53 val to_string
: t
-> string
55 val compare
: t
-> t
-> int
56 val equal
: t
-> t
-> bool
59 module type NumericType
= sig
73 val modulo
: t
-> t
-> t
77 let generic_pow zero one div_two mod_two
( * ) =
80 else if n
= one
then a
82 let b = pow a
(div_two n
) in
83 b * b * (if mod_two n
= zero
then one
else a
)
92 let succ, pred
, abs
= succ, pred
, abs
94 let add, sub
, mul
, div
= (+), (-), ( * ), (/)
96 let modulo a
b = a
mod b
97 let pow = generic_pow 0 1 (fun n
-> n
asr 1) (fun n
-> n
land 1) ( * )
99 let min_num, max_num
= min_int
, max_int
101 let equal (a
:int) b = a
= b
103 let of_int (n
:int) = n
104 let to_int (n
:int) = n
105 let of_string = int_of_string
106 let to_string = string_of_int
109 module Float
= struct
111 let zero, one
= 0., 1.
118 let add, sub
, mul
, div
= (+.), (-.), ( *.), (/.)
119 let modulo = mod_float
122 let min_num, max_num
= neg_infinity
, infinity
123 let compare = compare
124 let epsilon = ref 0.00001
125 let set_precision e
= epsilon := e
126 let equal a
b = abs(b-.a
) < !epsilon
128 let of_int = float_of_int
129 let to_int = int_of_float
131 let of_string = float_of_string
132 let to_string = string_of_float