Project

General

Profile

Download (1.33 KB) Statistics
| Branch: | Tag: | Revision:
1 de8e9811 ploc
(* (a, b, c) means a * 10^-b. c is the original string *)
2 ca7ff3f7 Lélio Brun
type t = Q.t * int * string
3 de8e9811 ploc
4 ca7e8027 Lélio Brun
let pp fmt (_, _, s) =
5 ca7ff3f7 Lélio Brun
  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 de8e9811 ploc
10 ef598ac3 ploc
let create m e s = Q.of_string m, e, s
11
12
let create_q q s = q, 0, s
13 ca7ff3f7 Lélio Brun
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 f0195e96 ploc
let rec to_q (c, e, s) =
18 ca7ff3f7 Lélio Brun
  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 f0195e96 ploc
23 ef598ac3 ploc
let to_num = to_q
24 ca7ff3f7 Lélio Brun
25 de8e9811 ploc
let to_string (_, _, s) = s
26 ca7ff3f7 Lélio Brun
27 ca7e8027 Lélio Brun
(* let eq r1 r2 =
28
 *   Q.equal (to_q r1) (to_q r2) *)
29 ca7ff3f7 Lélio Brun
30 de8e9811 ploc
let num_binop op r1 r2 =
31
  let n1 = to_num r1 and n2 = to_num r2 in
32
  op n1 n2
33 ca7ff3f7 Lélio Brun
34 de8e9811 ploc
let arith_binop op r1 r2 =
35
  let r = num_binop op r1 r2 in
36 ef598ac3 ploc
  create_q r (Q.to_string r)
37 ca7ff3f7 Lélio Brun
38
let add = arith_binop Q.add
39
40 ef598ac3 ploc
let minus = arith_binop Q.sub
41 ca7ff3f7 Lélio Brun
42 ef598ac3 ploc
let times = arith_binop Q.mul
43 ca7ff3f7 Lélio Brun
44
let div = arith_binop Q.div
45 de8e9811 ploc
46 ef598ac3 ploc
let uminus (c, e, s) = Q.neg c, e, "-" ^ s
47 de8e9811 ploc
48 ca7ff3f7 Lélio Brun
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 de8e9811 ploc
60 ef598ac3 ploc
let zero = Q.zero, 0, "0.0"
61 de8e9811 ploc
62 ef598ac3 ploc
let is_zero r = Q.equal (to_num r) Q.zero
63 ca7ff3f7 Lélio Brun
64 ef598ac3 ploc
let is_one r = Q.equal (to_num r) Q.one