Project

General

Profile

Revision de8e9811

View differences:

src/real.ml
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)
src/real.mli
1
type t
2
val pp: Format.formatter -> t -> unit
3
val pp_ada: Format.formatter -> t -> unit
4
val create: string -> int -> string -> t
5
val create_num: Num.num -> string -> t
6

  
7
val add: t -> t -> t
8
val minus: t -> t -> t
9
val times: t -> t -> t
10
val div: t -> t -> t
11
val uminus: t -> t
12

  
13
val lt: t -> t -> bool
14
val le: t -> t -> bool
15
val gt: t -> t -> bool
16
val ge: t -> t -> bool
17
val eq: t -> t -> bool
18
val diseq: t -> t -> bool
19
  
20
val to_num: t -> Num.num
21
val to_string: t -> string
22
val eq: t -> t -> bool
23
val zero: t
24

  
25
val is_zero: t -> bool
26
val is_one: t -> bool

Also available in: Unified diff