Source file sol_nb.ml
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
type sol_nb = (float * int)
(** This type is used to represent the number of solutions of a
bdd. The idea is to approximate this number of solutions by
[a.2**n] where [a] is null or a float inside the interval [[1, 2[], and
[n] a positive integer. The reason for that logarithmic encoding
is that the number of solutions of a formula is exponential in the
number of variables; this means that number of solutions is likely
to trigger overflow errors.
In order to compute the [sol_nb] of a bdd using the ones of its
sub-branches, we take advantage of the following equality:
(1) [a.2**(n+p) + b.2**n = 2**(-k).(a+b.2**(-p)).2**(n+p+k)]
where [k] is the smallest integer such that:
[2**(-k).(a+b.2**(-p)) < 2].
*)
let float_of_sol_nb (a, n) = a *. (2.** (float_of_int n))
let string_of_sol_nb sol = string_of_float (float_of_sol_nb sol)
let rec (add_sol_nb: sol_nb -> sol_nb -> sol_nb) =
fun (a, n) (b, m) ->
if a = 0. then (b, m)
else if b = 0. then (a, n)
else let _ = assert ((a >= 1.) && (a < 2.) && (b >= 1.) && (b < 2.)) in
if
(n > m)
then
add_sol_nb (b, m) (a, n)
else
let p = m - n in
let temp = b +. a *. (2.0**(float_of_int (-p))) in
let k = (floor (((log temp) /. (log 2.0)) -. 1.)) +. 1. in
let new_cst = temp *. (2.0)**(-. k) in
let _ = assert ((1.0 <= new_cst) || new_cst = 0.) in
let _ = assert ((new_cst < 2.0) || new_cst = 0.) in
(new_cst, (m+(int_of_float k)))
let _ = assert ((add_sol_nb (1., 0) (1., 0)) = (1., 1))
let _ = assert ((add_sol_nb (1., 1) (1., 1)) = (1., 2))
let _ = assert ((add_sol_nb (1., 5) (1., 5)) = (1., 6))
let _ = assert ((add_sol_nb (1., 2) (1., 3)) = (1.5, 3))
let _ = assert ((add_sol_nb (1.5, 1) (1., 2)) = (1.75, 2))
let _ = assert ((add_sol_nb (1., 0) (0., 1)) = (1., 0))
let _ = assert ((add_sol_nb (1.453, 45) (0., 1)) = (1.453, 45))
let mult_sol_nb (a,n) (b,m) =
let t = a *. b in
if t < 2. then (t, n+m)
else (t /. 2. , n+m+1)
let _ = assert ((mult_sol_nb (1.5, 1) (1.5, 3)) = (1.125, 5))
let div_sol_nb (a,n) (b,m) =
let t = a /. b in
if t < 2. then (t, n-m)
else (t /. 2. , n-m+1)
let zero_sol = (0.0, 1)
let one_sol = (1., 0)
let eq_sol_nb = (=)
let two_power_of m = (1.0, m)
let sol_nb_of_float f =
let (x, n) = frexp f in
(x *. 2.0, n-1)
let _ = assert ((float_of_sol_nb (sol_nb_of_float 123456.0)) = 123456.0)
let _ = assert ((float_of_sol_nb (sol_nb_of_float 0.0)) = 0.0)
let big = 2.0 ** 300.
let _ = assert ((float_of_sol_nb (sol_nb_of_float big)) = big)
let _very_big = 2.0 ** 3000.