1
|
(* (a, b, c) means a * 10^-b. c is the original string *)
|
2
|
type t = Q.t * int * string
|
3
|
|
4
|
let pp fmt (_, _, s) =
|
5
|
Format.fprintf fmt "%s%s" s
|
6
|
(if String.get s (-1 + String.length s) = '.' then "0" else "")
|
7
|
|
8
|
let pp_ada fmt (c, e, _) = Format.fprintf fmt "%s.0*1.0e-%i" (Q.to_string c) e
|
9
|
|
10
|
let create m e s = Q.of_string m, e, s
|
11
|
|
12
|
let create_q q s = q, 0, s
|
13
|
|
14
|
(* let to_num (c, e, s) = let num_10 = Num.num_of_int 10 in Num.(c // (num_10
|
15
|
**/ (num_of_int e))) *)
|
16
|
|
17
|
let rec to_q (c, e, s) =
|
18
|
if e = 0 then c
|
19
|
else if e > 0 then Q.div (to_q (c, e - 1, s)) (Q.of_int 10)
|
20
|
else (* if exp<0 then *)
|
21
|
Q.mul (to_q (c, e + 1, s)) (Q.of_int 10)
|
22
|
|
23
|
let to_num = to_q
|
24
|
|
25
|
let to_string (_, _, s) = s
|
26
|
|
27
|
(* let eq r1 r2 =
|
28
|
* Q.equal (to_q r1) (to_q r2) *)
|
29
|
|
30
|
let num_binop op r1 r2 =
|
31
|
let n1 = to_num r1 and n2 = to_num r2 in
|
32
|
op n1 n2
|
33
|
|
34
|
let arith_binop op r1 r2 =
|
35
|
let r = num_binop op r1 r2 in
|
36
|
create_q r (Q.to_string r)
|
37
|
|
38
|
let add = arith_binop Q.add
|
39
|
|
40
|
let minus = arith_binop Q.sub
|
41
|
|
42
|
let times = arith_binop Q.mul
|
43
|
|
44
|
let div = arith_binop Q.div
|
45
|
|
46
|
let uminus (c, e, s) = Q.neg c, e, "-" ^ s
|
47
|
|
48
|
let lt = num_binop Q.( < )
|
49
|
|
50
|
let le = num_binop Q.( <= )
|
51
|
|
52
|
let gt = num_binop Q.( > )
|
53
|
|
54
|
let ge = num_binop Q.( >= )
|
55
|
|
56
|
let diseq = num_binop Q.( <> )
|
57
|
|
58
|
let eq = num_binop Q.( = )
|
59
|
|
60
|
let zero = Q.zero, 0, "0.0"
|
61
|
|
62
|
let is_zero r = Q.equal (to_num r) Q.zero
|
63
|
|
64
|
let is_one r = Q.equal (to_num r) Q.one
|