blob: f30dd8c001c61b7dd0749bf06efb2e0919e58cb9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
module type COMPARABLE = sig
type t
val eq: t -> t -> bool
val neq: t -> t -> bool
val lt: t -> t -> bool
val le: t -> t -> bool
val gt: t -> t -> bool
val ge: t -> t -> bool
end
module Comparable = struct
let eq = (=)
let neq = (<>)
let lt = (<)
let le = (<=)
let gt = (>)
let ge = (>=)
end
module Num = struct
let rnd () =
let value = Random.bits () in
Q.make (Z.of_int value) (Z.of_int (1 lsl 30))
include Q
let is_integer t = (Q.den t) == Z.one
let eq = Q.equal
let neq a b = not (Q.equal a b)
let mult = Q.mul
let floor t =
let num = Q.num t
and den = Q.den t in
if is_integer t then
Q.of_bigint num
else
Q.of_bigint @@ Z.fdiv num den
let round_down t =
let num = Q.num t
and den = Q.den t in
if is_integer t then
Q.of_bigint num
else
Q.of_bigint @@ Z.div num den
let round t =
if is_integer t then
t
else
let t' = match Q.sign t with
| 1 -> Q.add t @@ Q.of_ints 1 2
| -1 -> Q.add t @@ Q.of_ints (-1) 2
| _ -> t in
let num = Q.num t'
and den = Q.den t' in
Q.of_bigint (Z.div num den)
let ge = Q.geq
let ge = Q.geq
let le = Q.leq
let pow t q_factor = begin
if is_integer q_factor then
let factor = Q.to_int q_factor
and num = Q.num t
and den = Q.den t in
Q.make (Z.pow num factor) (Z.pow den factor)
else
let factor = Q.to_float q_factor
and num = Z.to_float @@ Q.num t
and den = Z.to_float @@ Q.den t in
Q.div
(Q.of_float (num ** factor))
(Q.of_float (den ** factor))
end
let gcd t1 t2 =
Q.of_bigint @@ Z.gcd (Q.to_bigint t1) (Q.to_bigint t2)
let lcm t1 t2 =
Q.of_bigint @@ Z.lcm (Q.to_bigint t1) (Q.to_bigint t2)
end
module Bool = struct
type t = bool
include Comparable
let true_ = true
let false_ = false
let or_ = (||)
let and_ = (&&)
let not = Pervasives.not
end
module String = struct
type t = UTF8.t
include Comparable
end
module Date = Date.Make(Num)
|