src/HOL/Matrix_LP/float_arith.ML
author wenzelm
Wed Nov 26 20:05:34 2014 +0100 (2014-11-26)
changeset 59058 a78612c67ec0
parent 56255 968667bbb8d2
child 69597 ff784d5a5bfb
permissions -rw-r--r--
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm@56255
     1
(*  Title:      HOL/Matrix_LP/float_arith.ML
wenzelm@56255
     2
    Author:     Steven Obua
haftmann@22964
     3
*)
haftmann@22964
     4
haftmann@22964
     5
signature FLOAT_ARITH =
haftmann@22964
     6
sig
haftmann@22964
     7
  exception Destruct_floatstr of string
haftmann@22964
     8
  val destruct_floatstr: (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string
haftmann@22964
     9
haftmann@22964
    10
  exception Floating_point of string
wenzelm@24630
    11
  val approx_dec_by_bin: int -> Float.float -> Float.float * Float.float
haftmann@22964
    12
  val approx_decstr_by_bin: int -> string -> Float.float * Float.float
haftmann@22964
    13
haftmann@22964
    14
  val mk_float: Float.float -> term
haftmann@22964
    15
  val dest_float: term -> Float.float
haftmann@22964
    16
haftmann@22964
    17
  val approx_float: int -> (Float.float * Float.float -> Float.float * Float.float)
haftmann@22964
    18
    -> string -> term * term
haftmann@22964
    19
end;
haftmann@22964
    20
haftmann@22964
    21
structure FloatArith : FLOAT_ARITH =
haftmann@22964
    22
struct
haftmann@22964
    23
haftmann@22964
    24
exception Destruct_floatstr of string;
haftmann@22964
    25
haftmann@22964
    26
fun destruct_floatstr isDigit isExp number =
haftmann@22964
    27
  let
haftmann@22964
    28
    val numlist = filter (not o Char.isSpace) (String.explode number)
haftmann@22964
    29
haftmann@22964
    30
    fun countsigns ((#"+")::cs) = countsigns cs
haftmann@22964
    31
      | countsigns ((#"-")::cs) =
haftmann@22964
    32
      let
haftmann@22964
    33
        val (positive, rest) = countsigns cs
haftmann@22964
    34
      in
haftmann@22964
    35
        (not positive, rest)
haftmann@22964
    36
      end
haftmann@22964
    37
      | countsigns cs = (true, cs)
haftmann@22964
    38
haftmann@22964
    39
    fun readdigits [] = ([], [])
haftmann@22964
    40
      | readdigits (q as c::cs) =
haftmann@22964
    41
      if (isDigit c) then
haftmann@22964
    42
        let
haftmann@22964
    43
          val (digits, rest) = readdigits cs
haftmann@22964
    44
        in
haftmann@22964
    45
          (c::digits, rest)
haftmann@22964
    46
        end
haftmann@22964
    47
      else
haftmann@22964
    48
        ([], q)
haftmann@22964
    49
haftmann@22964
    50
    fun readfromexp_helper cs =
haftmann@22964
    51
      let
haftmann@22964
    52
        val (positive, rest) = countsigns cs
haftmann@22964
    53
        val (digits, rest') = readdigits rest
haftmann@22964
    54
      in
haftmann@22964
    55
        case rest' of
haftmann@22964
    56
          [] => (positive, digits)
haftmann@22964
    57
          | _ => raise (Destruct_floatstr number)
haftmann@22964
    58
      end
haftmann@22964
    59
haftmann@22964
    60
    fun readfromexp [] = (true, [])
haftmann@22964
    61
      | readfromexp (c::cs) =
haftmann@22964
    62
      if isExp c then
haftmann@22964
    63
        readfromexp_helper cs
haftmann@22964
    64
      else
haftmann@22964
    65
        raise (Destruct_floatstr number)
haftmann@22964
    66
haftmann@22964
    67
    fun readfromdot [] = ([], readfromexp [])
haftmann@22964
    68
      | readfromdot ((#".")::cs) =
haftmann@22964
    69
      let
haftmann@22964
    70
        val (digits, rest) = readdigits cs
haftmann@22964
    71
        val exp = readfromexp rest
haftmann@22964
    72
      in
haftmann@22964
    73
        (digits, exp)
haftmann@22964
    74
      end
haftmann@22964
    75
      | readfromdot cs = readfromdot ((#".")::cs)
haftmann@22964
    76
haftmann@22964
    77
    val (positive, numlist) = countsigns numlist
haftmann@22964
    78
    val (digits1, numlist) = readdigits numlist
haftmann@22964
    79
     val (digits2, exp) = readfromdot numlist
haftmann@22964
    80
  in
haftmann@22964
    81
    (positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp))
haftmann@22964
    82
  end
haftmann@22964
    83
haftmann@22964
    84
exception Floating_point of string;
haftmann@22964
    85
haftmann@22964
    86
val ln2_10 = Math.ln 10.0 / Math.ln 2.0;
obua@23569
    87
fun exp5 x = Integer.pow x 5;
obua@23569
    88
fun exp10 x = Integer.pow x 10;
obua@25300
    89
fun exp2 x = Integer.pow x 2;
haftmann@22964
    90
haftmann@22964
    91
fun find_most_significant q r =
haftmann@22964
    92
  let
haftmann@22964
    93
    fun int2real i =
wenzelm@24630
    94
      case (Real.fromString o 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@24630
    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@24630
   103
      if 0 <= d then
obua@25300
   104
        (exp2 d, 0)
haftmann@22964
   105
      else
wenzelm@23297
   106
        (exp5 (~ d), d)
haftmann@22964
   107
wenzelm@24630
   108
    val L = Real.floor (int2real (IntInf.log2 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@24630
   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@24630
   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@24630
   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
obua@25300
   136
      | addseq acc d' (d::ds) = addseq (acc + exp2 (d - d')) d' ds
haftmann@22964
   137
wenzelm@23297
   138
    fun seq2bin [] = (0, 0)
wenzelm@24630
   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@24630
   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)
obua@25300
   154
              val x2 = (fst x1 * exp2 (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@24630
   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@24630
   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@24630
   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@24630
   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@24630
   194
    fun str2int s = the_default 0 (Int.fromString s)
wenzelm@24630
   195
    fun signint p x = if p then x else ~ 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
wenzelm@24630
   198
    val s = 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
wenzelm@24630
   203
    approx_dec_by_bin n (q,r)
haftmann@22964
   204
  end
haftmann@22964
   205
haftmann@22964
   206
fun mk_float (a, b) = @{term "float"} $
wenzelm@59058
   207
  HOLogic.mk_prod (apply2 (HOLogic.mk_number HOLogic.intT) (a, b));
haftmann@22964
   208
wenzelm@56255
   209
fun dest_float (Const (@{const_name float}, _) $ (Const (@{const_name Pair}, _) $ a $ b)) =
wenzelm@59058
   210
      apply2 (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;