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