Project

General

Profile

« Previous | Next » 

Revision ca7ff3f7

Added by LĂ©lio Brun 7 months ago

reformatting

View differences:

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

  
4 4
let pp fmt (_, _, s) =
5
    Format.fprintf fmt "%s%s"
6
      s
7
      (if String.get s (-1 + String.length s) = '.' then "0" else "")
5
  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
8 9

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

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

  
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

  
22 17
let rec to_q (c, e, s) =
23
  if e = 0 then
24
    c
25
  else
26
    if e > 0 then Q.div (to_q (c,e-1,s)) (Q.of_int 10)
27
    else (* if exp<0 then *)
28
      Q.mul
29
        (to_q (c,e+1,s))
30
        (Q.of_int 10)
18
  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)
31 22

  
32 23
let to_num = to_q
33
           
24

  
34 25
let to_string (_, _, s) = s
35
                        
26

  
36 27
(* let eq r1 r2 =
37 28
 *   Q.equal (to_q r1) (to_q r2) *)
38
  
39
  
29

  
40 30
let num_binop op r1 r2 =
41 31
  let n1 = to_num r1 and n2 = to_num r2 in
42 32
  op n1 n2
43
  
33

  
44 34
let arith_binop op r1 r2 =
45 35
  let r = num_binop op r1 r2 in
46 36
  create_q r (Q.to_string r)
47
  
48
let add   = arith_binop Q.add
37

  
38
let add = arith_binop Q.add
39

  
49 40
let minus = arith_binop Q.sub
41

  
50 42
let times = arith_binop Q.mul
51
let div   = arith_binop Q.div 
43

  
44
let div = arith_binop Q.div
52 45

  
53 46
let uminus (c, e, s) = Q.neg c, e, "-" ^ s
54 47

  
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.(=))
48
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.( = )
61 59

  
62 60
let zero = Q.zero, 0, "0.0"
63 61

  
64 62
let is_zero r = Q.equal (to_num r) Q.zero
63

  
65 64
let is_one r = Q.equal (to_num r) Q.one

Also available in: Unified diff