Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / real.ml @ f0195e96

History | View | Annotate | Download (1.53 KB)

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

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

    
9
let pp_ada fmt (c, e, s) =
10
  Format.fprintf fmt "%s.0*1.0e-%i" (Num.string_of_num c) e
11
  
12
let create m e s = Num.num_of_string m, e, s
13
let create_num n s = n, 0, s
14
                   
15
let to_num (c, e, s) =
16
  let num_10 = Num.num_of_int 10 in
17
  Num.(c // (num_10 **/ (num_of_int e)))
18

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

    
29
let to_string (_, _, s) = s
30
                        
31
let eq r1 r2 =
32
  Num.eq_num (to_num r1) (to_num r2)
33
  
34
  
35
let num_binop op r1 r2 =
36
  let n1 = to_num r1 and n2 = to_num r2 in
37
  op n1 n2
38
  
39
let arith_binop op r1 r2 =
40
  let r = num_binop op r1 r2 in
41
  create_num r (Num.string_of_num r)
42
  
43
let add   = arith_binop (Num.(+/))
44
let minus = arith_binop (Num.(-/))
45
let times = arith_binop (Num.( */))
46
let div   = arith_binop (Num.(//)) 
47

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

    
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.(=/))
56

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

    
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)