Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / real.ml @ de8e9811

History | View | Annotate | Download (1.3 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 to_string (_, _, s) = s
20
                        
21
let eq r1 r2 =
22
  Num.eq_num (to_num r1) (to_num r2)
23
  
24
  
25
let num_binop op r1 r2 =
26
  let n1 = to_num r1 and n2 = to_num r2 in
27
  op n1 n2
28
  
29
let arith_binop op r1 r2 =
30
  let r = num_binop op r1 r2 in
31
  create_num r (Num.string_of_num r)
32
  
33
let add   = arith_binop (Num.(+/))
34
let minus = arith_binop (Num.(-/))
35
let times = arith_binop (Num.( */))
36
let div   = arith_binop (Num.(//)) 
37

    
38
let uminus (c, e, s) = Num.minus_num c, e, "-" ^ s
39

    
40
let lt = num_binop (Num.(</))
41
let le = num_binop (Num.(<=/))
42
let gt = num_binop (Num.(>/))
43
let ge = num_binop (Num.(>=/))
44
let diseq = num_binop (Num.(<>/))
45
let eq = num_binop (Num.(=/))
46

    
47
let zero = Num.num_of_int 0, 0, "0.0"
48

    
49
let is_zero r = Num.eq_num (to_num r) (Num.num_of_int 0)
50
let is_one r = Num.eq_num (to_num r) (Num.num_of_int 1)