1414 You should have received a copy of the GNU General Public License along with
1515 this program. If not, see <https://www.gnu.org/licenses/>. *)
1616
17- module type NumberInterface = sig
17+ module type NumberInterfaceNoCompare = sig
1818 type t
1919
2020 val format_t : Format .formatter -> t -> unit
@@ -72,7 +72,28 @@ module type NumberInterface = sig
7272 val is_zero : t -> bool
7373end
7474
75- module RegularFloatNumber : NumberInterface = struct
75+ module type NumberInterface = sig
76+ include NumberInterfaceNoCompare
77+
78+ val compare : Com .comp_op -> t -> t -> bool
79+ end
80+
81+ module MakeComparable (N : NumberInterfaceNoCompare ) : NumberInterface = struct
82+ include N
83+
84+ let compare op i1 i2 =
85+ let epsilon = of_float ! Config. comparison_error_margin in
86+ let open Com in
87+ match op with
88+ | Gt -> i1 > . i2 +. epsilon
89+ | Gte -> i1 > . i2 -. epsilon
90+ | Lt -> i1 +. epsilon < . i2
91+ | Lte -> i1 -. epsilon < . i2
92+ | Eq -> abs (i1 -. i2) < . epsilon
93+ | Neq -> abs (i1 -. i2) > =. epsilon
94+ end
95+
96+ module RegularFloatNumber : NumberInterface = MakeComparable (struct
7697 type t = float
7798
7899 let format_t fmt f = Format. fprintf fmt " %f" f
@@ -138,7 +159,7 @@ module RegularFloatNumber : NumberInterface = struct
138159 let is_nan_or_inf x = not (Float. is_finite x)
139160
140161 let is_zero x = x = 0.
141- end
162+ end )
142163
143164let mpfr_abs (x : Mpfrf.t ) : Mpfrf.t =
144165 let out = Mpfr. init2 (Mpfr. get_prec x) in
@@ -155,7 +176,7 @@ let mpfr_ceil (x : Mpfrf.t) : Mpfrf.t =
155176 ignore (Mpfr. ceil out x);
156177 Mpfrf. of_mpfr out
157178
158- module MPFRNumber : NumberInterface = struct
179+ module MPFRNumber : NumberInterface = MakeComparable ( struct
159180 type t = Mpfrf .t
160181
161182 let rounding : Mpfr.round = Near
@@ -214,9 +235,9 @@ module MPFRNumber : NumberInterface = struct
214235 let is_zero x = x =. zero ()
215236
216237 let is_nan_or_inf x = not (Mpfrf. number_p x)
217- end
238+ end )
218239
219- module IntervalNumber : NumberInterface = struct
240+ module IntervalNumber : NumberInterface = MakeComparable ( struct
220241 type t = { down : Mpfrf .t ; up : Mpfrf .t }
221242
222243 let v (x : Mpfrf.t ) (y : Mpfrf.t ) : t = { down = x; up = y }
@@ -335,9 +356,9 @@ module IntervalNumber : NumberInterface = struct
335356 let is_zero x = x =. zero ()
336357
337358 let is_nan_or_inf x = not (Mpfrf. number_p x.down && Mpfrf. number_p x.up)
338- end
359+ end )
339360
340- module RationalNumber : NumberInterface = struct
361+ module RationalNumber : NumberInterface = MakeComparable ( struct
341362 type t = Mpqf .t
342363
343364 let format_t fmt f = Mpqf. print fmt f
@@ -407,11 +428,11 @@ module RationalNumber : NumberInterface = struct
407428 || Mpzf. cmp (Mpqf. get_den x) max > 0
408429 || Mpzf. cmp (Mpqf. get_num x) min < 0
409430 || Mpzf. cmp (Mpqf. get_den x) min < 0
410- end
431+ end )
411432
412433module BigIntFixedPointNumber (P : sig
413434 val scaling_factor_bits : int ref
414- end ) : NumberInterface = struct
435+ end ) : NumberInterface = MakeComparable ( struct
415436 type t = Mpzf .t
416437
417438 let precision_modulo () =
@@ -498,4 +519,4 @@ end) : NumberInterface = struct
498519 let max x y = if x > . y then x else y
499520
500521 let is_nan_or_inf _ = false
501- end
522+ end )
0 commit comments