1 (* Title: HOL/Real/float_arith.ML
6 signature FLOAT_ARITH =
8 exception Destruct_floatstr of string
9 val destruct_floatstr: (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string
11 exception Floating_point of string
12 val approx_dec_by_bin: integer -> Float.float -> Float.float * Float.float
13 val approx_decstr_by_bin: int -> string -> Float.float * Float.float
15 val mk_float: Float.float -> term
16 val dest_float: term -> Float.float
18 val approx_float: int -> (Float.float * Float.float -> Float.float * Float.float)
19 -> string -> term * term
22 structure FloatArith : FLOAT_ARITH =
25 exception Destruct_floatstr of string;
27 fun destruct_floatstr isDigit isExp number =
29 val numlist = filter (not o Char.isSpace) (String.explode number)
31 fun countsigns ((#"+")::cs) = countsigns cs
32 | countsigns ((#"-")::cs) =
34 val (positive, rest) = countsigns cs
38 | countsigns cs = (true, cs)
40 fun readdigits [] = ([], [])
41 | readdigits (q as c::cs) =
44 val (digits, rest) = readdigits cs
51 fun readfromexp_helper cs =
53 val (positive, rest) = countsigns cs
54 val (digits, rest') = readdigits rest
57 [] => (positive, digits)
58 | _ => raise (Destruct_floatstr number)
61 fun readfromexp [] = (true, [])
62 | readfromexp (c::cs) =
66 raise (Destruct_floatstr number)
68 fun readfromdot [] = ([], readfromexp [])
69 | readfromdot ((#".")::cs) =
71 val (digits, rest) = readdigits cs
72 val exp = readfromexp rest
76 | readfromdot cs = readfromdot ((#".")::cs)
78 val (positive, numlist) = countsigns numlist
79 val (digits1, numlist) = readdigits numlist
80 val (digits2, exp) = readfromdot numlist
82 (positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp))
85 exception Floating_point of string;
87 val ln2_10 = Math.ln 10.0 / Math.ln 2.0;
88 fun exp5 x = Integer.pow x 5;
89 fun exp10 x = Integer.pow x 10;
91 fun find_most_significant q r =
94 case (Real.fromString o Integer.string_of_int) i of
96 | NONE => raise (Floating_point "int2real")
97 fun subtract (q, r) (q', r') =
99 (q - q' * exp10 (r' - r), r)
101 (q * exp10 (r - r') - q', r')
108 val L = Integer.int (Real.floor (int2real (Integer.log q) + int2real r * ln2_10))
111 val (q1, r1) = subtract (q, r) (bin2dec L1)
115 val (q2, r2) = subtract (q, r) (bin2dec (L1 + 1))
118 raise (Floating_point "find_most_significant")
124 val (q0, r0) = subtract (q, r) (bin2dec L)
129 raise (Floating_point "find_most_significant")
133 fun approx_dec_by_bin n (q,r) =
135 fun addseq acc d' [] = acc
136 | addseq acc d' (d::ds) = addseq (acc +% Integer.exp (d - d')) d' ds
138 fun seq2bin [] = (0, 0)
139 | seq2bin (d::ds) = (addseq 0 d ds +% 1, d)
141 fun approx d_seq d0 precision (q,r) =
143 let val x = seq2bin d_seq in
148 val (d, (q', r')) = find_most_significant q r
150 if precision <% d0 - d then
152 val d' = d0 - precision
153 val x1 = seq2bin (d_seq)
154 val x2 = (fst x1 * Integer.exp (snd x1 - d') + 1, d') (* = seq2bin (d'::d_seq) *)
159 approx (d::d_seq) d0 precision (q', r')
162 fun approx_start precision (q, r) =
167 val (d, (q', r')) = find_most_significant q r
169 if precision <=% 0 then
176 (x1, seq2bin [d +% 1])
179 approx [d] d precision (q', r')
186 val ((a1,b1), (a2, b2)) = approx_start n (~ q, r)
188 ((~ a2, b2), (~ a1, b1))
192 fun approx_decstr_by_bin n decstr =
194 fun str2int s = the_default 0 (Integer.int_of_string s);
195 fun signint p x = if p then x else Integer.neg x
197 val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr
198 val s = Integer.int (size d2)
200 val q = signint p (str2int d1 * exp10 s + str2int d2)
201 val r = signint ep (str2int e) - s
203 approx_dec_by_bin (Integer.int n) (q,r)
206 fun mk_float (a, b) = @{term "float"} $
207 HOLogic.mk_prod (pairself (HOLogic.mk_number HOLogic.intT) (a, b));
209 fun dest_float (Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) =
210 pairself (snd o HOLogic.dest_number) (a, b)
211 | dest_float t = ((snd o HOLogic.dest_number) t, 0);
213 fun approx_float prec f value =
215 val interval = approx_decstr_by_bin prec value
216 val (flower, fupper) = f interval
218 (mk_float flower, mk_float fupper)