Project

General

Profile

Revision ef598ac3 src/real.ml

View differences:

src/real.ml
1 1
(* (a, b, c) means a * 10^-b. c is the original string *)
2
type t = Num.num * int * string 
2
type t = Q.t * int * string 
3 3

  
4 4
let pp fmt (c, e, s) =
5 5
    Format.fprintf fmt "%s%s"
......
7 7
      (if String.get s (-1 + String.length s) = '.' then "0" else "")
8 8

  
9 9
let pp_ada fmt (c, e, s) =
10
  Format.fprintf fmt "%s.0*1.0e-%i" (Num.string_of_num c) e
10
  Format.fprintf fmt "%s.0*1.0e-%i" (Q.to_string c) e
11 11
  
12
let create m e s = Num.num_of_string m, e, s
13
let create_num n s = n, 0, s
12
let create m e s = Q.of_string m, e, s
13

  
14
let create_q q s = q, 0, s
14 15
                   
16
(*
15 17
let to_num (c, e, s) =
16 18
  let num_10 = Num.num_of_int 10 in
17 19
  Num.(c // (num_10 **/ (num_of_int e)))
18

  
20
 *)
21
                 
19 22
let rec to_q (c, e, s) =
20 23
  if e = 0 then
21
        Q.of_string (Num.string_of_num c)
24
    c
22 25
  else
23 26
    if e > 0 then Q.div (to_q (c,e-1,s)) (Q.of_int 10)
24 27
    else (* if exp<0 then *)
......
26 29
        (to_q (c,e+1,s))
27 30
        (Q.of_int 10)
28 31

  
32
let to_num = to_q
33
           
29 34
let to_string (_, _, s) = s
30 35
                        
31 36
let eq r1 r2 =
32
  Num.eq_num (to_num r1) (to_num r2)
37
  Q.equal (to_q r1) (to_q r2)
33 38
  
34 39
  
35 40
let num_binop op r1 r2 =
......
38 43
  
39 44
let arith_binop op r1 r2 =
40 45
  let r = num_binop op r1 r2 in
41
  create_num r (Num.string_of_num r)
46
  create_q r (Q.to_string r)
42 47
  
43
let add   = arith_binop (Num.(+/))
44
let minus = arith_binop (Num.(-/))
45
let times = arith_binop (Num.( */))
46
let div   = arith_binop (Num.(//)) 
48
let add   = arith_binop Q.add
49
let minus = arith_binop Q.sub
50
let times = arith_binop Q.mul
51
let div   = arith_binop Q.div 
47 52

  
48
let uminus (c, e, s) = Num.minus_num c, e, "-" ^ s
53
let uminus (c, e, s) = Q.neg c, e, "-" ^ s
49 54

  
50
let lt = num_binop (Num.(</))
51
let le = num_binop (Num.(<=/))
52
let gt = num_binop (Num.(>/))
53
let ge = num_binop (Num.(>=/))
54
let diseq = num_binop (Num.(<>/))
55
let eq = num_binop (Num.(=/))
55
let lt = num_binop (Q.(<))
56
let le = num_binop (Q.(<=))
57
let gt = num_binop (Q.(>))
58
let ge = num_binop (Q.(>=))
59
let diseq = num_binop (Q.(<>))
60
let eq = num_binop (Q.(=))
56 61

  
57
let zero = Num.num_of_int 0, 0, "0.0"
62
let zero = Q.zero, 0, "0.0"
58 63

  
59
let is_zero r = Num.eq_num (to_num r) (Num.num_of_int 0)
60
let is_one r = Num.eq_num (to_num r) (Num.num_of_int 1)
64
let is_zero r = Q.equal (to_num r) Q.zero
65
let is_one r = Q.equal (to_num r) Q.one

Also available in: Unified diff