src/HOL/Real/float_arith.ML
author haftmann
Tue Jun 05 19:19:30 2007 +0200 (2007-06-05)
changeset 23261 85f27f79232f
parent 22964 2284e0d02e7f
child 23297 06f108974fa1
permissions -rw-r--r--
tuned integers
     1 (*  Title: HOL/Real/Float.ML
     2     ID:    $Id$
     3     Author: Steven Obua
     4 *)
     5 
     6 signature FLOAT_ARITH =
     7 sig
     8   exception Destruct_floatstr of string
     9   val destruct_floatstr: (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string
    10 
    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
    14 
    15   val mk_float: Float.float -> term
    16   val dest_float: term -> Float.float
    17 
    18   val approx_float: int -> (Float.float * Float.float -> Float.float * Float.float)
    19     -> string -> term * term
    20 end;
    21 
    22 structure FloatArith : FLOAT_ARITH =
    23 struct
    24 
    25 exception Destruct_floatstr of string;
    26 
    27 fun destruct_floatstr isDigit isExp number =
    28   let
    29     val numlist = filter (not o Char.isSpace) (String.explode number)
    30 
    31     fun countsigns ((#"+")::cs) = countsigns cs
    32       | countsigns ((#"-")::cs) =
    33       let
    34         val (positive, rest) = countsigns cs
    35       in
    36         (not positive, rest)
    37       end
    38       | countsigns cs = (true, cs)
    39 
    40     fun readdigits [] = ([], [])
    41       | readdigits (q as c::cs) =
    42       if (isDigit c) then
    43         let
    44           val (digits, rest) = readdigits cs
    45         in
    46           (c::digits, rest)
    47         end
    48       else
    49         ([], q)
    50 
    51     fun readfromexp_helper cs =
    52       let
    53         val (positive, rest) = countsigns cs
    54         val (digits, rest') = readdigits rest
    55       in
    56         case rest' of
    57           [] => (positive, digits)
    58           | _ => raise (Destruct_floatstr number)
    59       end
    60 
    61     fun readfromexp [] = (true, [])
    62       | readfromexp (c::cs) =
    63       if isExp c then
    64         readfromexp_helper cs
    65       else
    66         raise (Destruct_floatstr number)
    67 
    68     fun readfromdot [] = ([], readfromexp [])
    69       | readfromdot ((#".")::cs) =
    70       let
    71         val (digits, rest) = readdigits cs
    72         val exp = readfromexp rest
    73       in
    74         (digits, exp)
    75       end
    76       | readfromdot cs = readfromdot ((#".")::cs)
    77 
    78     val (positive, numlist) = countsigns numlist
    79     val (digits1, numlist) = readdigits numlist
    80      val (digits2, exp) = readfromdot numlist
    81   in
    82     (positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp))
    83   end
    84 
    85 exception Floating_point of string;
    86 
    87 val ln2_10 = Math.ln 10.0 / Math.ln 2.0;
    88 val exp5 = Integer.pow (Integer.int 5);
    89 val exp10 = Integer.pow (Integer.int 10);
    90 
    91 fun intle a b = not (Integer.cmp (a, b) = GREATER);
    92 fun intless a b = Integer.cmp (a, b) = LESS;
    93 
    94 fun find_most_significant q r =
    95   let
    96     fun int2real i =
    97       case (Real.fromString o Integer.string_of_int) i of
    98         SOME r => r
    99         | NONE => raise (Floating_point "int2real")
   100     fun subtract (q, r) (q', r') =
   101       if intle r r' then
   102         (Integer.sub q (Integer.mult q' (exp10 (Integer.sub r' r))), r)
   103       else
   104         (Integer.sub (Integer.mult q (exp10 (Integer.sub r r'))) q', r')
   105     fun bin2dec d =
   106       if intle Integer.zero d then
   107         (Integer.exp d, Integer.zero)
   108       else
   109         (exp5 (Integer.neg d), d)
   110 
   111     val L = Integer.int (Real.floor (int2real (Integer.log q) + int2real r * ln2_10))
   112     val L1 = Integer.inc L
   113 
   114     val (q1, r1) = subtract (q, r) (bin2dec L1) 
   115   in
   116     if intle Integer.zero q1 then
   117       let
   118         val (q2, r2) = subtract (q, r) (bin2dec (Integer.inc L1))
   119       in
   120         if intle Integer.zero q2 then
   121           raise (Floating_point "find_most_significant")
   122         else
   123           (L1, (q1, r1))
   124       end
   125     else
   126       let
   127         val (q0, r0) = subtract (q, r) (bin2dec L)
   128       in
   129         if intle Integer.zero q0 then
   130           (L, (q0, r0))
   131         else
   132           raise (Floating_point "find_most_significant")
   133       end
   134   end
   135 
   136 fun approx_dec_by_bin n (q,r) =
   137   let
   138     fun addseq acc d' [] = acc
   139       | addseq acc d' (d::ds) = addseq (Integer.add acc (Integer.exp (Integer.sub d d'))) d' ds
   140 
   141     fun seq2bin [] = (Integer.zero, Integer.zero)
   142       | seq2bin (d::ds) = (Integer.inc (addseq Integer.zero d ds), d)
   143 
   144     fun approx d_seq d0 precision (q,r) =
   145       if q = Integer.zero then
   146         let val x = seq2bin d_seq in
   147           (x, x)
   148         end
   149       else
   150         let
   151           val (d, (q', r')) = find_most_significant q r
   152         in
   153           if intless precision (Integer.sub d0 d) then
   154             let
   155               val d' = Integer.sub d0 precision
   156               val x1 = seq2bin (d_seq)
   157               val x2 = (Integer.inc
   158                 (Integer.mult (fst x1)
   159                 (Integer.exp (Integer.sub (snd x1) d'))),  d') (* = seq2bin (d'::d_seq) *)
   160             in
   161               (x1, x2)
   162             end
   163           else
   164             approx (d::d_seq) d0 precision (q', r')
   165         end
   166 
   167     fun approx_start precision (q, r) =
   168       if q = Integer.zero then
   169         ((Integer.zero, Integer.zero), (Integer.zero, Integer.zero))
   170       else
   171         let
   172           val (d, (q', r')) = find_most_significant q r
   173         in
   174           if intle precision Integer.zero then
   175             let
   176               val x1 = seq2bin [d]
   177             in
   178               if q' = Integer.zero then
   179                 (x1, x1)
   180               else
   181                 (x1, seq2bin [Integer.inc d])
   182             end
   183           else
   184             approx [d] d precision (q', r')
   185         end
   186   in
   187     if intle Integer.zero q then
   188       approx_start n (q,r)
   189     else
   190       let
   191         val ((a1,b1), (a2, b2)) = approx_start n (Integer.neg q, r)
   192       in
   193         ((Integer.neg a2, b2), (Integer.neg a1, b1))
   194       end
   195   end
   196 
   197 fun approx_decstr_by_bin n decstr =
   198   let
   199     fun str2int s = the_default Integer.zero (Integer.int_of_string s);
   200     fun signint p x = if p then x else Integer.neg x
   201 
   202     val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr
   203     val s = Integer.int (size d2)
   204 
   205     val q = signint p (Integer.add (Integer.mult (str2int d1) (exp10 s)) (str2int d2))
   206     val r = Integer.sub (signint ep (str2int e)) s
   207   in
   208     approx_dec_by_bin (Integer.int n) (q,r)
   209   end
   210 
   211 fun mk_float (a, b) = @{term "float"} $
   212   HOLogic.mk_prod (pairself (HOLogic.mk_number HOLogic.intT) (a, b));
   213 
   214 fun dest_float (Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) =
   215       pairself (snd o HOLogic.dest_number) (a, b)
   216   | dest_float t = ((snd o HOLogic.dest_number) t, Integer.zero);
   217 
   218 fun approx_float prec f value =
   219   let
   220     val interval = approx_decstr_by_bin prec value
   221     val (flower, fupper) = f interval
   222   in
   223     (mk_float flower, mk_float fupper)
   224   end;
   225 
   226 end;