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