src/HOL/Real/float_arith.ML
author wenzelm
Sat Jun 09 00:28:46 2007 +0200 (2007-06-09)
changeset 23297 06f108974fa1
parent 23261 85f27f79232f
child 23569 6be49c181c66
permissions -rw-r--r--
simplified type integer;
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;
wenzelm@23297
    88
val exp5 = Integer.pow 5;
wenzelm@23297
    89
val exp10 = Integer.pow 10;
haftmann@22964
    90
haftmann@22964
    91
fun find_most_significant q r =
haftmann@22964
    92
  let
haftmann@22964
    93
    fun int2real i =
haftmann@23261
    94
      case (Real.fromString o Integer.string_of_int) i of
haftmann@22964
    95
        SOME r => r
haftmann@22964
    96
        | NONE => raise (Floating_point "int2real")
haftmann@22964
    97
    fun subtract (q, r) (q', r') =
wenzelm@23297
    98
      if r <=% r' then
wenzelm@23297
    99
        (q - q' * exp10 (r' - r), r)
haftmann@22964
   100
      else
wenzelm@23297
   101
        (q * exp10 (r - r') - q', r')
haftmann@22964
   102
    fun bin2dec d =
wenzelm@23297
   103
      if 0 <=% d then
wenzelm@23297
   104
        (Integer.exp d, 0)
haftmann@22964
   105
      else
wenzelm@23297
   106
        (exp5 (~ d), d)
haftmann@22964
   107
haftmann@23261
   108
    val L = Integer.int (Real.floor (int2real (Integer.log q) + int2real r * ln2_10))
wenzelm@23297
   109
    val L1 = L + 1
haftmann@22964
   110
haftmann@22964
   111
    val (q1, r1) = subtract (q, r) (bin2dec L1) 
haftmann@22964
   112
  in
wenzelm@23297
   113
    if 0 <=% q1 then
haftmann@22964
   114
      let
wenzelm@23297
   115
        val (q2, r2) = subtract (q, r) (bin2dec (L1 + 1))
haftmann@22964
   116
      in
wenzelm@23297
   117
        if 0 <=% q2 then
haftmann@22964
   118
          raise (Floating_point "find_most_significant")
haftmann@22964
   119
        else
haftmann@22964
   120
          (L1, (q1, r1))
haftmann@22964
   121
      end
haftmann@22964
   122
    else
haftmann@22964
   123
      let
haftmann@22964
   124
        val (q0, r0) = subtract (q, r) (bin2dec L)
haftmann@22964
   125
      in
wenzelm@23297
   126
        if 0 <=% q0 then
haftmann@22964
   127
          (L, (q0, r0))
haftmann@22964
   128
        else
haftmann@22964
   129
          raise (Floating_point "find_most_significant")
haftmann@22964
   130
      end
haftmann@22964
   131
  end
haftmann@22964
   132
haftmann@22964
   133
fun approx_dec_by_bin n (q,r) =
haftmann@22964
   134
  let
haftmann@22964
   135
    fun addseq acc d' [] = acc
wenzelm@23297
   136
      | addseq acc d' (d::ds) = addseq (acc +% Integer.exp (d - d')) d' ds
haftmann@22964
   137
wenzelm@23297
   138
    fun seq2bin [] = (0, 0)
wenzelm@23297
   139
      | seq2bin (d::ds) = (addseq 0 d ds +% 1, d)
haftmann@22964
   140
haftmann@22964
   141
    fun approx d_seq d0 precision (q,r) =
wenzelm@23297
   142
      if q = 0 then
haftmann@22964
   143
        let val x = seq2bin d_seq in
haftmann@22964
   144
          (x, x)
haftmann@22964
   145
        end
haftmann@22964
   146
      else
haftmann@22964
   147
        let
haftmann@22964
   148
          val (d, (q', r')) = find_most_significant q r
haftmann@22964
   149
        in
wenzelm@23297
   150
          if precision <% d0 - d then
haftmann@22964
   151
            let
wenzelm@23297
   152
              val d' = d0 - precision
haftmann@22964
   153
              val x1 = seq2bin (d_seq)
wenzelm@23297
   154
              val x2 = (fst x1 * Integer.exp (snd x1 - d') + 1,  d') (* = seq2bin (d'::d_seq) *)
haftmann@22964
   155
            in
haftmann@22964
   156
              (x1, x2)
haftmann@22964
   157
            end
haftmann@22964
   158
          else
haftmann@22964
   159
            approx (d::d_seq) d0 precision (q', r')
haftmann@22964
   160
        end
haftmann@22964
   161
haftmann@22964
   162
    fun approx_start precision (q, r) =
wenzelm@23297
   163
      if q =% 0 then
wenzelm@23297
   164
        ((0, 0), (0, 0))
haftmann@22964
   165
      else
haftmann@22964
   166
        let
haftmann@22964
   167
          val (d, (q', r')) = find_most_significant q r
haftmann@22964
   168
        in
wenzelm@23297
   169
          if precision <=% 0 then
haftmann@22964
   170
            let
haftmann@22964
   171
              val x1 = seq2bin [d]
haftmann@22964
   172
            in
wenzelm@23297
   173
              if q' = 0 then
haftmann@22964
   174
                (x1, x1)
haftmann@22964
   175
              else
wenzelm@23297
   176
                (x1, seq2bin [d +% 1])
haftmann@22964
   177
            end
haftmann@22964
   178
          else
haftmann@22964
   179
            approx [d] d precision (q', r')
haftmann@22964
   180
        end
haftmann@22964
   181
  in
wenzelm@23297
   182
    if 0 <=% q then
haftmann@22964
   183
      approx_start n (q,r)
haftmann@22964
   184
    else
haftmann@22964
   185
      let
wenzelm@23297
   186
        val ((a1,b1), (a2, b2)) = approx_start n (~ q, r)
haftmann@22964
   187
      in
wenzelm@23297
   188
        ((~ a2, b2), (~ a1, b1))
haftmann@22964
   189
      end
haftmann@22964
   190
  end
haftmann@22964
   191
haftmann@22964
   192
fun approx_decstr_by_bin n decstr =
haftmann@22964
   193
  let
wenzelm@23297
   194
    fun str2int s = the_default 0 (Integer.int_of_string s);
haftmann@23261
   195
    fun signint p x = if p then x else Integer.neg x
haftmann@22964
   196
haftmann@22964
   197
    val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr
haftmann@23261
   198
    val s = Integer.int (size d2)
haftmann@22964
   199
wenzelm@23297
   200
    val q = signint p (str2int d1 * exp10 s + str2int d2)
wenzelm@23297
   201
    val r = signint ep (str2int e) - s
haftmann@22964
   202
  in
haftmann@23261
   203
    approx_dec_by_bin (Integer.int n) (q,r)
haftmann@22964
   204
  end
haftmann@22964
   205
haftmann@22964
   206
fun mk_float (a, b) = @{term "float"} $
haftmann@22964
   207
  HOLogic.mk_prod (pairself (HOLogic.mk_number HOLogic.intT) (a, b));
haftmann@22964
   208
haftmann@22964
   209
fun dest_float (Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) =
haftmann@22964
   210
      pairself (snd o HOLogic.dest_number) (a, b)
wenzelm@23297
   211
  | dest_float t = ((snd o HOLogic.dest_number) t, 0);
haftmann@22964
   212
haftmann@22964
   213
fun approx_float prec f value =
haftmann@22964
   214
  let
haftmann@22964
   215
    val interval = approx_decstr_by_bin prec value
haftmann@22964
   216
    val (flower, fupper) = f interval
haftmann@22964
   217
  in
haftmann@22964
   218
    (mk_float flower, mk_float fupper)
haftmann@22964
   219
  end;
haftmann@22964
   220
haftmann@22964
   221
end;