src/HOL/Library/Float.thy
author wenzelm
Tue May 15 13:57:39 2018 +0200 (16 months ago)
changeset 68189 6163c90694ef
parent 67573 ed0a7090167d
child 68406 6beb45f6cf67
permissions -rw-r--r--
tuned headers;
hoelzl@47615
     1
(*  Title:      HOL/Library/Float.thy
hoelzl@47615
     2
    Author:     Johannes Hölzl, Fabian Immler
hoelzl@47615
     3
    Copyright   2012  TU München
hoelzl@47615
     4
*)
hoelzl@47615
     5
wenzelm@60500
     6
section \<open>Floating-Point Numbers\<close>
huffman@29988
     7
haftmann@20485
     8
theory Float
nipkow@63663
     9
imports Log_Nat Lattice_Algebras
haftmann@20485
    10
begin
obua@16782
    11
wenzelm@49812
    12
definition "float = {m * 2 powr e | (m :: int) (e :: int). True}"
wenzelm@49812
    13
wenzelm@49834
    14
typedef float = float
hoelzl@47599
    15
  morphisms real_of_float float_of
wenzelm@49812
    16
  unfolding float_def by auto
obua@16782
    17
lp15@61609
    18
setup_lifting type_definition_float
hoelzl@47601
    19
lp15@61609
    20
declare real_of_float [code_unfold]
hoelzl@47599
    21
hoelzl@47599
    22
lemmas float_of_inject[simp]
hoelzl@47599
    23
lp15@61609
    24
declare [[coercion "real_of_float :: float \<Rightarrow> real"]]
hoelzl@47600
    25
wenzelm@63356
    26
lemma real_of_float_eq: "f1 = f2 \<longleftrightarrow> real_of_float f1 = real_of_float f2" for f1 f2 :: float
lp15@61609
    27
  unfolding real_of_float_inject ..
hoelzl@47599
    28
lp15@61609
    29
declare real_of_float_inverse[simp] float_of_inverse [simp]
lp15@61609
    30
declare real_of_float [simp]
obua@16782
    31
wenzelm@63356
    32
wenzelm@60500
    33
subsection \<open>Real operations preserving the representation as floating point number\<close>
hoelzl@47599
    34
wenzelm@63356
    35
lemma floatI: "m * 2 powr e = x \<Longrightarrow> x \<in> float" for m e :: int
hoelzl@47599
    36
  by (auto simp: float_def)
wenzelm@19765
    37
wenzelm@60698
    38
lemma zero_float[simp]: "0 \<in> float"
wenzelm@60698
    39
  by (auto simp: float_def)
wenzelm@63356
    40
wenzelm@60698
    41
lemma one_float[simp]: "1 \<in> float"
wenzelm@60698
    42
  by (intro floatI[of 1 0]) simp
wenzelm@63356
    43
wenzelm@60698
    44
lemma numeral_float[simp]: "numeral i \<in> float"
wenzelm@60698
    45
  by (intro floatI[of "numeral i" 0]) simp
wenzelm@63356
    46
wenzelm@60698
    47
lemma neg_numeral_float[simp]: "- numeral i \<in> float"
wenzelm@60698
    48
  by (intro floatI[of "- numeral i" 0]) simp
wenzelm@63356
    49
wenzelm@63356
    50
lemma real_of_int_float[simp]: "real_of_int x \<in> float" for x :: int
wenzelm@60698
    51
  by (intro floatI[of x 0]) simp
wenzelm@63356
    52
wenzelm@63356
    53
lemma real_of_nat_float[simp]: "real x \<in> float" for x :: nat
wenzelm@60698
    54
  by (intro floatI[of x 0]) simp
wenzelm@63356
    55
wenzelm@63356
    56
lemma two_powr_int_float[simp]: "2 powr (real_of_int i) \<in> float" for i :: int
wenzelm@60698
    57
  by (intro floatI[of 1 i]) simp
wenzelm@63356
    58
wenzelm@63356
    59
lemma two_powr_nat_float[simp]: "2 powr (real i) \<in> float" for i :: nat
wenzelm@60698
    60
  by (intro floatI[of 1 i]) simp
wenzelm@63356
    61
wenzelm@63356
    62
lemma two_powr_minus_int_float[simp]: "2 powr - (real_of_int i) \<in> float" for i :: int
wenzelm@60698
    63
  by (intro floatI[of 1 "-i"]) simp
wenzelm@63356
    64
wenzelm@63356
    65
lemma two_powr_minus_nat_float[simp]: "2 powr - (real i) \<in> float" for i :: nat
wenzelm@60698
    66
  by (intro floatI[of 1 "-i"]) simp
wenzelm@63356
    67
wenzelm@60698
    68
lemma two_powr_numeral_float[simp]: "2 powr numeral i \<in> float"
wenzelm@60698
    69
  by (intro floatI[of 1 "numeral i"]) simp
wenzelm@63356
    70
wenzelm@60698
    71
lemma two_powr_neg_numeral_float[simp]: "2 powr - numeral i \<in> float"
wenzelm@60698
    72
  by (intro floatI[of 1 "- numeral i"]) simp
wenzelm@63356
    73
wenzelm@60698
    74
lemma two_pow_float[simp]: "2 ^ n \<in> float"
wenzelm@63356
    75
  by (intro floatI[of 1 n]) (simp add: powr_realpow)
lp15@61609
    76
hoelzl@45495
    77
hoelzl@47599
    78
lemma plus_float[simp]: "r \<in> float \<Longrightarrow> p \<in> float \<Longrightarrow> r + p \<in> float"
hoelzl@47599
    79
  unfolding float_def
hoelzl@47599
    80
proof (safe, simp)
wenzelm@60698
    81
  have *: "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
wenzelm@60698
    82
    if "e1 \<le> e2" for e1 m1 e2 m2 :: int
wenzelm@60698
    83
  proof -
wenzelm@60698
    84
    from that have "m1 * 2 powr e1 + m2 * 2 powr e2 = (m1 + m2 * 2 ^ nat (e2 - e1)) * 2 powr e1"
lp15@65583
    85
      by (simp add: powr_realpow[symmetric] powr_diff field_simps)
wenzelm@60698
    86
    then show ?thesis
wenzelm@60698
    87
      by blast
wenzelm@60698
    88
  qed
wenzelm@60698
    89
  fix e1 m1 e2 m2 :: int
wenzelm@60698
    90
  consider "e2 \<le> e1" | "e1 \<le> e2" by (rule linorder_le_cases)
wenzelm@60698
    91
  then show "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
wenzelm@60698
    92
  proof cases
wenzelm@60698
    93
    case 1
wenzelm@60698
    94
    from *[OF this, of m2 m1] show ?thesis
wenzelm@60698
    95
      by (simp add: ac_simps)
wenzelm@60698
    96
  next
wenzelm@60698
    97
    case 2
wenzelm@60698
    98
    then show ?thesis by (rule *)
wenzelm@60698
    99
  qed
hoelzl@47599
   100
qed
obua@16782
   101
hoelzl@47599
   102
lemma uminus_float[simp]: "x \<in> float \<Longrightarrow> -x \<in> float"
hoelzl@47599
   103
  apply (auto simp: float_def)
thomas@57492
   104
  apply hypsubst_thin
lp15@60017
   105
  apply (rename_tac m e)
lp15@60017
   106
  apply (rule_tac x="-m" in exI)
lp15@60017
   107
  apply (rule_tac x="e" in exI)
hoelzl@47599
   108
  apply (simp add: field_simps)
hoelzl@47599
   109
  done
hoelzl@29804
   110
hoelzl@47599
   111
lemma times_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x * y \<in> float"
hoelzl@47599
   112
  apply (auto simp: float_def)
thomas@57492
   113
  apply hypsubst_thin
lp15@60017
   114
  apply (rename_tac mx my ex ey)
lp15@60017
   115
  apply (rule_tac x="mx * my" in exI)
lp15@60017
   116
  apply (rule_tac x="ex + ey" in exI)
hoelzl@47599
   117
  apply (simp add: powr_add)
hoelzl@47599
   118
  done
hoelzl@29804
   119
hoelzl@47599
   120
lemma minus_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x - y \<in> float"
haftmann@54230
   121
  using plus_float [of x "- y"] by simp
hoelzl@47599
   122
wenzelm@61945
   123
lemma abs_float[simp]: "x \<in> float \<Longrightarrow> \<bar>x\<bar> \<in> float"
hoelzl@47599
   124
  by (cases x rule: linorder_cases[of 0]) auto
hoelzl@47599
   125
hoelzl@47599
   126
lemma sgn_of_float[simp]: "x \<in> float \<Longrightarrow> sgn x \<in> float"
hoelzl@47599
   127
  by (cases x rule: linorder_cases[of 0]) (auto intro!: uminus_float)
wenzelm@21404
   128
hoelzl@47599
   129
lemma div_power_2_float[simp]: "x \<in> float \<Longrightarrow> x / 2^d \<in> float"
hoelzl@47599
   130
  apply (auto simp add: float_def)
thomas@57492
   131
  apply hypsubst_thin
lp15@60017
   132
  apply (rename_tac m e)
lp15@60017
   133
  apply (rule_tac x="m" in exI)
lp15@60017
   134
  apply (rule_tac x="e - d" in exI)
hoelzl@47599
   135
  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
hoelzl@47599
   136
  done
hoelzl@47599
   137
hoelzl@47599
   138
lemma div_power_2_int_float[simp]: "x \<in> float \<Longrightarrow> x / (2::int)^d \<in> float"
hoelzl@47599
   139
  apply (auto simp add: float_def)
thomas@57492
   140
  apply hypsubst_thin
lp15@60017
   141
  apply (rename_tac m e)
lp15@60017
   142
  apply (rule_tac x="m" in exI)
lp15@60017
   143
  apply (rule_tac x="e - d" in exI)
hoelzl@47599
   144
  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
hoelzl@47599
   145
  done
obua@16782
   146
hoelzl@47599
   147
lemma div_numeral_Bit0_float[simp]:
wenzelm@63356
   148
  assumes "x / numeral n \<in> float"
wenzelm@60698
   149
  shows "x / (numeral (Num.Bit0 n)) \<in> float"
hoelzl@47599
   150
proof -
hoelzl@47599
   151
  have "(x / numeral n) / 2^1 \<in> float"
wenzelm@63356
   152
    by (intro assms div_power_2_float)
hoelzl@47599
   153
  also have "(x / numeral n) / 2^1 = x / (numeral (Num.Bit0 n))"
hoelzl@47599
   154
    by (induct n) auto
hoelzl@47599
   155
  finally show ?thesis .
hoelzl@47599
   156
qed
hoelzl@47599
   157
hoelzl@47599
   158
lemma div_neg_numeral_Bit0_float[simp]:
wenzelm@63356
   159
  assumes "x / numeral n \<in> float"
wenzelm@60698
   160
  shows "x / (- numeral (Num.Bit0 n)) \<in> float"
hoelzl@47599
   161
proof -
wenzelm@60698
   162
  have "- (x / numeral (Num.Bit0 n)) \<in> float"
wenzelm@63356
   163
    using assms by simp
haftmann@54489
   164
  also have "- (x / numeral (Num.Bit0 n)) = x / - numeral (Num.Bit0 n)"
haftmann@54489
   165
    by simp
hoelzl@47599
   166
  finally show ?thesis .
hoelzl@29804
   167
qed
obua@16782
   168
wenzelm@60698
   169
lemma power_float[simp]:
wenzelm@60698
   170
  assumes "a \<in> float"
wenzelm@60698
   171
  shows "a ^ b \<in> float"
immler@58985
   172
proof -
wenzelm@60698
   173
  from assms obtain m e :: int where "a = m * 2 powr e"
immler@58985
   174
    by (auto simp: float_def)
wenzelm@60698
   175
  then show ?thesis
immler@58985
   176
    by (auto intro!: floatI[where m="m^b" and e = "e*b"]
immler@58985
   177
      simp: power_mult_distrib powr_realpow[symmetric] powr_powr)
immler@58985
   178
qed
immler@58985
   179
wenzelm@60698
   180
lift_definition Float :: "int \<Rightarrow> int \<Rightarrow> float" is "\<lambda>(m::int) (e::int). m * 2 powr e"
wenzelm@60698
   181
  by simp
hoelzl@47601
   182
declare Float.rep_eq[simp]
hoelzl@47601
   183
immler@62419
   184
code_datatype Float
immler@62419
   185
hoelzl@47780
   186
lemma compute_real_of_float[code]:
hoelzl@47780
   187
  "real_of_float (Float m e) = (if e \<ge> 0 then m * 2 ^ nat e else m / 2 ^ (nat (-e)))"
lp15@61609
   188
  by (simp add: powr_int)
hoelzl@47780
   189
wenzelm@60698
   190
wenzelm@60500
   191
subsection \<open>Arithmetic operations on floating point numbers\<close>
hoelzl@47599
   192
wenzelm@63356
   193
instantiation float :: "{ring_1,linorder,linordered_ring,linordered_idom,numeral,equal}"
hoelzl@47599
   194
begin
hoelzl@47599
   195
hoelzl@47600
   196
lift_definition zero_float :: float is 0 by simp
hoelzl@47601
   197
declare zero_float.rep_eq[simp]
wenzelm@63356
   198
hoelzl@47600
   199
lift_definition one_float :: float is 1 by simp
hoelzl@47601
   200
declare one_float.rep_eq[simp]
wenzelm@63356
   201
nipkow@67399
   202
lift_definition plus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "(+)" by simp
hoelzl@47601
   203
declare plus_float.rep_eq[simp]
wenzelm@63356
   204
nipkow@67399
   205
lift_definition times_float :: "float \<Rightarrow> float \<Rightarrow> float" is "( * )" by simp
hoelzl@47601
   206
declare times_float.rep_eq[simp]
wenzelm@63356
   207
nipkow@67399
   208
lift_definition minus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "(-)" by simp
hoelzl@47601
   209
declare minus_float.rep_eq[simp]
wenzelm@63356
   210
hoelzl@47600
   211
lift_definition uminus_float :: "float \<Rightarrow> float" is "uminus" by simp
hoelzl@47601
   212
declare uminus_float.rep_eq[simp]
hoelzl@47599
   213
hoelzl@47600
   214
lift_definition abs_float :: "float \<Rightarrow> float" is abs by simp
hoelzl@47601
   215
declare abs_float.rep_eq[simp]
wenzelm@63356
   216
hoelzl@47600
   217
lift_definition sgn_float :: "float \<Rightarrow> float" is sgn by simp
hoelzl@47601
   218
declare sgn_float.rep_eq[simp]
obua@16782
   219
nipkow@67399
   220
lift_definition equal_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(=) :: real \<Rightarrow> real \<Rightarrow> bool" .
hoelzl@47599
   221
nipkow@67399
   222
lift_definition less_eq_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(\<le>)" .
hoelzl@47601
   223
declare less_eq_float.rep_eq[simp]
wenzelm@63356
   224
nipkow@67399
   225
lift_definition less_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(<)" .
hoelzl@47601
   226
declare less_float.rep_eq[simp]
obua@16782
   227
hoelzl@47599
   228
instance
wenzelm@63356
   229
  by standard (transfer; fastforce simp add: field_simps intro: mult_left_mono mult_right_mono)+
wenzelm@60698
   230
hoelzl@47599
   231
end
hoelzl@29804
   232
lp15@61639
   233
lemma real_of_float [simp]: "real_of_float (of_nat n) = of_nat n"
wenzelm@63356
   234
  by (induct n) simp_all
lp15@61639
   235
lp15@61639
   236
lemma real_of_float_of_int_eq [simp]: "real_of_float (of_int z) = of_int z"
lp15@61639
   237
  by (cases z rule: int_diff_cases) (simp_all add: of_rat_diff)
lp15@61639
   238
immler@58985
   239
lemma Float_0_eq_0[simp]: "Float 0 e = 0"
immler@58985
   240
  by transfer simp
immler@58985
   241
wenzelm@63356
   242
lemma real_of_float_power[simp]: "real_of_float (f^n) = real_of_float f^n" for f :: float
hoelzl@47599
   243
  by (induct n) simp_all
hoelzl@45495
   244
wenzelm@63356
   245
lemma real_of_float_min: "real_of_float (min x y) = min (real_of_float x) (real_of_float y)"
wenzelm@63356
   246
  and real_of_float_max: "real_of_float (max x y) = max (real_of_float x) (real_of_float y)"
wenzelm@63356
   247
  for x y :: float
hoelzl@47600
   248
  by (simp_all add: min_def max_def)
hoelzl@47599
   249
hoelzl@53215
   250
instance float :: unbounded_dense_linorder
hoelzl@47599
   251
proof
hoelzl@47599
   252
  fix a b :: float
hoelzl@47599
   253
  show "\<exists>c. a < c"
hoelzl@47599
   254
    apply (intro exI[of _ "a + 1"])
hoelzl@47600
   255
    apply transfer
hoelzl@47599
   256
    apply simp
hoelzl@47599
   257
    done
hoelzl@47599
   258
  show "\<exists>c. c < a"
hoelzl@47599
   259
    apply (intro exI[of _ "a - 1"])
hoelzl@47600
   260
    apply transfer
hoelzl@47599
   261
    apply simp
hoelzl@47599
   262
    done
wenzelm@60698
   263
  show "\<exists>c. a < c \<and> c < b" if "a < b"
wenzelm@60698
   264
    apply (rule exI[of _ "(a + b) * Float 1 (- 1)"])
wenzelm@60698
   265
    using that
hoelzl@47600
   266
    apply transfer
haftmann@54489
   267
    apply (simp add: powr_minus)
hoelzl@29804
   268
    done
hoelzl@29804
   269
qed
hoelzl@29804
   270
hoelzl@47600
   271
instantiation float :: lattice_ab_group_add
wenzelm@46573
   272
begin
hoelzl@47599
   273
wenzelm@60698
   274
definition inf_float :: "float \<Rightarrow> float \<Rightarrow> float"
wenzelm@60698
   275
  where "inf_float a b = min a b"
hoelzl@29804
   276
wenzelm@60698
   277
definition sup_float :: "float \<Rightarrow> float \<Rightarrow> float"
wenzelm@60698
   278
  where "sup_float a b = max a b"
hoelzl@29804
   279
hoelzl@47599
   280
instance
wenzelm@63356
   281
  by standard (transfer; simp add: inf_float_def sup_float_def real_of_float_min real_of_float_max)+
wenzelm@60679
   282
hoelzl@29804
   283
end
hoelzl@29804
   284
lp15@61609
   285
lemma float_numeral[simp]: "real_of_float (numeral x :: float) = numeral x"
hoelzl@47600
   286
  apply (induct x)
hoelzl@47600
   287
  apply simp
lp15@61609
   288
  apply (simp_all only: numeral_Bit0 numeral_Bit1 real_of_float_eq float_of_inverse
wenzelm@63356
   289
          plus_float.rep_eq one_float.rep_eq plus_float numeral_float one_float)
hoelzl@47600
   290
  done
hoelzl@29804
   291
wenzelm@53381
   292
lemma transfer_numeral [transfer_rule]:
nipkow@67399
   293
  "rel_fun (=) pcr_float (numeral :: _ \<Rightarrow> real) (numeral :: _ \<Rightarrow> float)"
wenzelm@60698
   294
  by (simp add: rel_fun_def float.pcr_cr_eq cr_float_def)
hoelzl@47599
   295
lp15@61609
   296
lemma float_neg_numeral[simp]: "real_of_float (- numeral x :: float) = - numeral x"
haftmann@54489
   297
  by simp
huffman@47108
   298
wenzelm@53381
   299
lemma transfer_neg_numeral [transfer_rule]:
nipkow@67399
   300
  "rel_fun (=) pcr_float (- numeral :: _ \<Rightarrow> real) (- numeral :: _ \<Rightarrow> float)"
wenzelm@60698
   301
  by (simp add: rel_fun_def float.pcr_cr_eq cr_float_def)
hoelzl@47600
   302
immler@67573
   303
lemma float_of_numeral: "numeral k = float_of (numeral k)"
immler@67573
   304
  and float_of_neg_numeral: "- numeral k = float_of (- numeral k)"
hoelzl@47600
   305
  unfolding real_of_float_eq by simp_all
huffman@47108
   306
wenzelm@60698
   307
wenzelm@60500
   308
subsection \<open>Quickcheck\<close>
immler@58987
   309
immler@58987
   310
instantiation float :: exhaustive
immler@58987
   311
begin
immler@58987
   312
immler@58987
   313
definition exhaustive_float where
immler@58987
   314
  "exhaustive_float f d =
wenzelm@63356
   315
    Quickcheck_Exhaustive.exhaustive (\<lambda>x. Quickcheck_Exhaustive.exhaustive (\<lambda>y. f (Float x y)) d) d"
immler@58987
   316
immler@58987
   317
instance ..
immler@58987
   318
immler@58987
   319
end
immler@58987
   320
immler@58987
   321
definition (in term_syntax) [code_unfold]:
immler@58987
   322
  "valtermify_float x y = Code_Evaluation.valtermify Float {\<cdot>} x {\<cdot>} y"
immler@58987
   323
immler@58987
   324
instantiation float :: full_exhaustive
immler@58987
   325
begin
immler@58987
   326
wenzelm@63356
   327
definition
immler@58987
   328
  "full_exhaustive_float f d =
immler@58987
   329
    Quickcheck_Exhaustive.full_exhaustive
immler@58987
   330
      (\<lambda>x. Quickcheck_Exhaustive.full_exhaustive (\<lambda>y. f (valtermify_float x y)) d) d"
immler@58987
   331
immler@58987
   332
instance ..
immler@58987
   333
immler@58987
   334
end
immler@58987
   335
immler@58987
   336
instantiation float :: random
immler@58987
   337
begin
immler@58987
   338
immler@58987
   339
definition "Quickcheck_Random.random i =
immler@58987
   340
  scomp (Quickcheck_Random.random (2 ^ nat_of_natural i))
immler@58987
   341
    (\<lambda>man. scomp (Quickcheck_Random.random i) (\<lambda>exp. Pair (valtermify_float man exp)))"
immler@58987
   342
immler@58987
   343
instance ..
immler@58987
   344
immler@58987
   345
end
immler@58987
   346
immler@58987
   347
wenzelm@60500
   348
subsection \<open>Represent floats as unique mantissa and exponent\<close>
huffman@47108
   349
hoelzl@47599
   350
lemma int_induct_abs[case_names less]:
hoelzl@47599
   351
  fixes j :: int
hoelzl@47599
   352
  assumes H: "\<And>n. (\<And>i. \<bar>i\<bar> < \<bar>n\<bar> \<Longrightarrow> P i) \<Longrightarrow> P n"
hoelzl@47599
   353
  shows "P j"
hoelzl@47599
   354
proof (induct "nat \<bar>j\<bar>" arbitrary: j rule: less_induct)
wenzelm@60698
   355
  case less
wenzelm@60698
   356
  show ?case by (rule H[OF less]) simp
hoelzl@47599
   357
qed
hoelzl@47599
   358
hoelzl@47599
   359
lemma int_cancel_factors:
wenzelm@60698
   360
  fixes n :: int
wenzelm@60698
   361
  assumes "1 < r"
wenzelm@60698
   362
  shows "n = 0 \<or> (\<exists>k i. n = k * r ^ i \<and> \<not> r dvd k)"
hoelzl@47599
   363
proof (induct n rule: int_induct_abs)
hoelzl@47599
   364
  case (less n)
wenzelm@60698
   365
  have "\<exists>k i. n = k * r ^ Suc i \<and> \<not> r dvd k" if "n \<noteq> 0" "n = m * r" for m
wenzelm@60698
   366
  proof -
wenzelm@60698
   367
    from that have "\<bar>m \<bar> < \<bar>n\<bar>"
wenzelm@60500
   368
      using \<open>1 < r\<close> by (simp add: abs_mult)
wenzelm@60698
   369
    from less[OF this] that show ?thesis by auto
wenzelm@60698
   370
  qed
hoelzl@47599
   371
  then show ?case
haftmann@59554
   372
    by (metis dvd_def monoid_mult_class.mult.right_neutral mult.commute power_0)
hoelzl@47599
   373
qed
hoelzl@47599
   374
hoelzl@47599
   375
lemma mult_powr_eq_mult_powr_iff_asym:
hoelzl@47599
   376
  fixes m1 m2 e1 e2 :: int
wenzelm@60698
   377
  assumes m1: "\<not> 2 dvd m1"
wenzelm@60698
   378
    and "e1 \<le> e2"
hoelzl@47599
   379
  shows "m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
wenzelm@60698
   380
  (is "?lhs \<longleftrightarrow> ?rhs")
hoelzl@47599
   381
proof
wenzelm@60698
   382
  show ?rhs if eq: ?lhs
wenzelm@60698
   383
  proof -
wenzelm@60698
   384
    have "m1 \<noteq> 0"
wenzelm@60698
   385
      using m1 unfolding dvd_def by auto
wenzelm@60698
   386
    from \<open>e1 \<le> e2\<close> eq have "m1 = m2 * 2 powr nat (e2 - e1)"
lp15@65583
   387
      by (simp add: powr_diff field_simps)
wenzelm@60698
   388
    also have "\<dots> = m2 * 2^nat (e2 - e1)"
wenzelm@60698
   389
      by (simp add: powr_realpow)
wenzelm@60698
   390
    finally have m1_eq: "m1 = m2 * 2^nat (e2 - e1)"
lp15@61649
   391
      by linarith
wenzelm@60698
   392
    with m1 have "m1 = m2"
wenzelm@60698
   393
      by (cases "nat (e2 - e1)") (auto simp add: dvd_def)
wenzelm@60698
   394
    then show ?thesis
wenzelm@60698
   395
      using eq \<open>m1 \<noteq> 0\<close> by (simp add: powr_inj)
wenzelm@60698
   396
  qed
wenzelm@60698
   397
  show ?lhs if ?rhs
wenzelm@60698
   398
    using that by simp
wenzelm@60698
   399
qed
hoelzl@47599
   400
hoelzl@47599
   401
lemma mult_powr_eq_mult_powr_iff:
wenzelm@63356
   402
  "\<not> 2 dvd m1 \<Longrightarrow> \<not> 2 dvd m2 \<Longrightarrow> m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
wenzelm@63356
   403
  for m1 m2 e1 e2 :: int
hoelzl@47599
   404
  using mult_powr_eq_mult_powr_iff_asym[of m1 e1 e2 m2]
hoelzl@47599
   405
  using mult_powr_eq_mult_powr_iff_asym[of m2 e2 e1 m1]
hoelzl@47599
   406
  by (cases e1 e2 rule: linorder_le_cases) auto
hoelzl@47599
   407
hoelzl@47599
   408
lemma floatE_normed:
hoelzl@47599
   409
  assumes x: "x \<in> float"
hoelzl@47599
   410
  obtains (zero) "x = 0"
hoelzl@47599
   411
   | (powr) m e :: int where "x = m * 2 powr e" "\<not> 2 dvd m" "x \<noteq> 0"
wenzelm@60698
   412
proof -
wenzelm@63356
   413
  have "\<exists>(m::int) (e::int). x = m * 2 powr e \<and> \<not> (2::int) dvd m" if "x \<noteq> 0"
wenzelm@63356
   414
  proof -
wenzelm@60698
   415
    from x obtain m e :: int where x: "x = m * 2 powr e"
wenzelm@60698
   416
      by (auto simp: float_def)
wenzelm@60500
   417
    with \<open>x \<noteq> 0\<close> int_cancel_factors[of 2 m] obtain k i where "m = k * 2 ^ i" "\<not> 2 dvd k"
hoelzl@47599
   418
      by auto
wenzelm@63356
   419
    with \<open>\<not> 2 dvd k\<close> x show ?thesis
wenzelm@63356
   420
      apply (rule_tac exI[of _ "k"])
wenzelm@63356
   421
      apply (rule_tac exI[of _ "e + int i"])
wenzelm@63356
   422
      apply (simp add: powr_add powr_realpow)
wenzelm@63356
   423
      done
wenzelm@63356
   424
  qed
wenzelm@60698
   425
  with that show thesis by blast
hoelzl@47599
   426
qed
hoelzl@47599
   427
hoelzl@47599
   428
lemma float_normed_cases:
hoelzl@47599
   429
  fixes f :: float
hoelzl@47599
   430
  obtains (zero) "f = 0"
lp15@61609
   431
   | (powr) m e :: int where "real_of_float f = m * 2 powr e" "\<not> 2 dvd m" "f \<noteq> 0"
hoelzl@47599
   432
proof (atomize_elim, induct f)
wenzelm@60698
   433
  case (float_of y)
wenzelm@60698
   434
  then show ?case
hoelzl@47600
   435
    by (cases rule: floatE_normed) (auto simp: zero_float_def)
hoelzl@47599
   436
qed
hoelzl@47599
   437
wenzelm@63356
   438
definition mantissa :: "float \<Rightarrow> int"
wenzelm@63356
   439
  where "mantissa f =
wenzelm@63356
   440
    fst (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or>
wenzelm@63356
   441
      (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p))"
hoelzl@47599
   442
wenzelm@63356
   443
definition exponent :: "float \<Rightarrow> int"
wenzelm@63356
   444
  where "exponent f =
wenzelm@63356
   445
    snd (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or>
wenzelm@63356
   446
      (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p))"
hoelzl@47599
   447
immler@67573
   448
lemma exponent_0[simp]: "exponent 0 = 0" (is ?E)
immler@67573
   449
  and mantissa_0[simp]: "mantissa 0 = 0" (is ?M)
hoelzl@47599
   450
proof -
wenzelm@60698
   451
  have "\<And>p::int \<times> int. fst p = 0 \<and> snd p = 0 \<longleftrightarrow> p = (0, 0)"
wenzelm@60698
   452
    by auto
hoelzl@47599
   453
  then show ?E ?M
hoelzl@47600
   454
    by (auto simp add: mantissa_def exponent_def zero_float_def)
hoelzl@29804
   455
qed
hoelzl@29804
   456
wenzelm@63356
   457
lemma mantissa_exponent: "real_of_float f = mantissa f * 2 powr exponent f" (is ?E)
immler@67573
   458
  and mantissa_not_dvd: "f \<noteq> 0 \<Longrightarrow> \<not> 2 dvd mantissa f" (is "_ \<Longrightarrow> ?D")
hoelzl@47599
   459
proof cases
immler@67573
   460
  assume [simp]: "f \<noteq> 0"
hoelzl@47599
   461
  have "f = mantissa f * 2 powr exponent f \<and> \<not> 2 dvd mantissa f"
hoelzl@47599
   462
  proof (cases f rule: float_normed_cases)
wenzelm@60698
   463
    case zero
immler@67573
   464
    then show ?thesis by simp
wenzelm@60698
   465
  next
hoelzl@47599
   466
    case (powr m e)
wenzelm@60698
   467
    then have "\<exists>p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or>
lp15@61609
   468
      (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p)"
hoelzl@47599
   469
      by auto
hoelzl@47599
   470
    then show ?thesis
hoelzl@47599
   471
      unfolding exponent_def mantissa_def
immler@67573
   472
      by (rule someI2_ex) simp
wenzelm@60698
   473
  qed
hoelzl@47599
   474
  then show ?E ?D by auto
hoelzl@47599
   475
qed simp
hoelzl@47599
   476
immler@67573
   477
lemma mantissa_noteq_0: "f \<noteq> 0 \<Longrightarrow> mantissa f \<noteq> 0"
hoelzl@47599
   478
  using mantissa_not_dvd[of f] by auto
hoelzl@47599
   479
immler@67573
   480
lemma mantissa_eq_zero_iff: "mantissa x = 0 \<longleftrightarrow> x = 0"
immler@67573
   481
  (is "?lhs \<longleftrightarrow> ?rhs")
immler@67573
   482
proof
immler@67573
   483
  show ?rhs if ?lhs
immler@67573
   484
  proof -
immler@67573
   485
    from that have z: "0 = real_of_float x"
immler@67573
   486
      using mantissa_exponent by simp
immler@67573
   487
    show ?thesis
immler@67573
   488
      by (simp add: zero_float_def z)
immler@67573
   489
  qed
immler@67573
   490
  show ?lhs if ?rhs
immler@67573
   491
    using that by simp
immler@67573
   492
qed
immler@67573
   493
immler@67573
   494
lemma mantissa_pos_iff: "0 < mantissa x \<longleftrightarrow> 0 < x"
immler@67573
   495
  by (auto simp: mantissa_exponent sign_simps)
immler@67573
   496
immler@67573
   497
lemma mantissa_nonneg_iff: "0 \<le> mantissa x \<longleftrightarrow> 0 \<le> x"
immler@67573
   498
  by (auto simp: mantissa_exponent sign_simps zero_le_mult_iff)
immler@67573
   499
immler@67573
   500
lemma mantissa_neg_iff: "0 > mantissa x \<longleftrightarrow> 0 > x"
immler@67573
   501
  by (auto simp: mantissa_exponent sign_simps zero_le_mult_iff)
immler@67573
   502
wenzelm@53381
   503
lemma
hoelzl@47599
   504
  fixes m e :: int
hoelzl@47599
   505
  defines "f \<equiv> float_of (m * 2 powr e)"
hoelzl@47599
   506
  assumes dvd: "\<not> 2 dvd m"
hoelzl@47599
   507
  shows mantissa_float: "mantissa f = m" (is "?M")
hoelzl@47599
   508
    and exponent_float: "m \<noteq> 0 \<Longrightarrow> exponent f = e" (is "_ \<Longrightarrow> ?E")
hoelzl@47599
   509
proof cases
wenzelm@60698
   510
  assume "m = 0"
wenzelm@60698
   511
  with dvd show "mantissa f = m" by auto
hoelzl@47599
   512
next
hoelzl@47599
   513
  assume "m \<noteq> 0"
immler@67573
   514
  then have f_not_0: "f \<noteq> 0" by (simp add: f_def zero_float_def)
wenzelm@60698
   515
  from mantissa_exponent[of f] have "m * 2 powr e = mantissa f * 2 powr exponent f"
hoelzl@47599
   516
    by (auto simp add: f_def)
wenzelm@63356
   517
  then show ?M ?E
hoelzl@47599
   518
    using mantissa_not_dvd[OF f_not_0] dvd
hoelzl@47599
   519
    by (auto simp: mult_powr_eq_mult_powr_iff)
hoelzl@47599
   520
qed
hoelzl@47599
   521
wenzelm@60698
   522
wenzelm@60500
   523
subsection \<open>Compute arithmetic operations\<close>
hoelzl@47600
   524
hoelzl@47600
   525
lemma Float_mantissa_exponent: "Float (mantissa f) (exponent f) = f"
hoelzl@47600
   526
  unfolding real_of_float_eq mantissa_exponent[of f] by simp
hoelzl@47600
   527
wenzelm@60698
   528
lemma Float_cases [cases type: float]:
hoelzl@47600
   529
  fixes f :: float
hoelzl@47600
   530
  obtains (Float) m e :: int where "f = Float m e"
hoelzl@47600
   531
  using Float_mantissa_exponent[symmetric]
hoelzl@47600
   532
  by (atomize_elim) auto
hoelzl@47600
   533
hoelzl@47599
   534
lemma denormalize_shift:
immler@67573
   535
  assumes f_def: "f = Float m e"
immler@67573
   536
    and not_0: "f \<noteq> 0"
hoelzl@47599
   537
  obtains i where "m = mantissa f * 2 ^ i" "e = exponent f - i"
hoelzl@47599
   538
proof
hoelzl@47599
   539
  from mantissa_exponent[of f] f_def
hoelzl@47599
   540
  have "m * 2 powr e = mantissa f * 2 powr exponent f"
hoelzl@47599
   541
    by simp
hoelzl@47599
   542
  then have eq: "m = mantissa f * 2 powr (exponent f - e)"
lp15@65583
   543
    by (simp add: powr_diff field_simps)
hoelzl@47599
   544
  moreover
hoelzl@47599
   545
  have "e \<le> exponent f"
hoelzl@47599
   546
  proof (rule ccontr)
hoelzl@47599
   547
    assume "\<not> e \<le> exponent f"
hoelzl@47599
   548
    then have pos: "exponent f < e" by simp
lp15@61609
   549
    then have "2 powr (exponent f - e) = 2 powr - real_of_int (e - exponent f)"
hoelzl@47599
   550
      by simp
hoelzl@47599
   551
    also have "\<dots> = 1 / 2^nat (e - exponent f)"
lp15@65583
   552
      using pos by (simp add: powr_realpow[symmetric] powr_diff)
lp15@61609
   553
    finally have "m * 2^nat (e - exponent f) = real_of_int (mantissa f)"
hoelzl@47599
   554
      using eq by simp
hoelzl@47599
   555
    then have "mantissa f = m * 2^nat (e - exponent f)"
lp15@61609
   556
      by linarith
wenzelm@60500
   557
    with \<open>exponent f < e\<close> have "2 dvd mantissa f"
hoelzl@47599
   558
      apply (intro dvdI[where k="m * 2^(nat (e-exponent f)) div 2"])
hoelzl@47599
   559
      apply (cases "nat (e - exponent f)")
hoelzl@47599
   560
      apply auto
hoelzl@47599
   561
      done
hoelzl@47599
   562
    then show False using mantissa_not_dvd[OF not_0] by simp
hoelzl@47599
   563
  qed
lp15@61609
   564
  ultimately have "real_of_int m = mantissa f * 2^nat (exponent f - e)"
hoelzl@47599
   565
    by (simp add: powr_realpow[symmetric])
wenzelm@60500
   566
  with \<open>e \<le> exponent f\<close>
wenzelm@63356
   567
  show "m = mantissa f * 2 ^ nat (exponent f - e)"
lp15@61649
   568
    by linarith
lp15@61649
   569
  show "e = exponent f - nat (exponent f - e)"
wenzelm@61799
   570
    using \<open>e \<le> exponent f\<close> by auto
hoelzl@29804
   571
qed
hoelzl@29804
   572
wenzelm@60698
   573
context
wenzelm@60698
   574
begin
hoelzl@47600
   575
wenzelm@60698
   576
qualified lemma compute_float_zero[code_unfold, code]: "0 = Float 0 0"
hoelzl@47600
   577
  by transfer simp
wenzelm@60698
   578
wenzelm@60698
   579
qualified lemma compute_float_one[code_unfold, code]: "1 = Float 1 0"
wenzelm@60698
   580
  by transfer simp
hoelzl@47600
   581
immler@58982
   582
lift_definition normfloat :: "float \<Rightarrow> float" is "\<lambda>x. x" .
immler@58982
   583
lemma normloat_id[simp]: "normfloat x = x" by transfer rule
hoelzl@47600
   584
wenzelm@63356
   585
qualified lemma compute_normfloat[code]:
wenzelm@63356
   586
  "normfloat (Float m e) =
wenzelm@63356
   587
    (if m mod 2 = 0 \<and> m \<noteq> 0 then normfloat (Float (m div 2) (e + 1))
wenzelm@63356
   588
     else if m = 0 then 0 else Float m e)"
hoelzl@47600
   589
  by transfer (auto simp add: powr_add zmod_eq_0_iff)
hoelzl@47599
   590
wenzelm@60698
   591
qualified lemma compute_float_numeral[code_abbrev]: "Float (numeral k) 0 = numeral k"
hoelzl@47600
   592
  by transfer simp
hoelzl@47599
   593
wenzelm@60698
   594
qualified lemma compute_float_neg_numeral[code_abbrev]: "Float (- numeral k) 0 = - numeral k"
hoelzl@47600
   595
  by transfer simp
hoelzl@47599
   596
wenzelm@60698
   597
qualified lemma compute_float_uminus[code]: "- Float m1 e1 = Float (- m1) e1"
hoelzl@47600
   598
  by transfer simp
hoelzl@47599
   599
wenzelm@60698
   600
qualified lemma compute_float_times[code]: "Float m1 e1 * Float m2 e2 = Float (m1 * m2) (e1 + e2)"
hoelzl@47600
   601
  by transfer (simp add: field_simps powr_add)
hoelzl@47599
   602
wenzelm@63356
   603
qualified lemma compute_float_plus[code]:
wenzelm@63356
   604
  "Float m1 e1 + Float m2 e2 =
wenzelm@63356
   605
    (if m1 = 0 then Float m2 e2
wenzelm@63356
   606
     else if m2 = 0 then Float m1 e1
wenzelm@63356
   607
     else if e1 \<le> e2 then Float (m1 + m2 * 2^nat (e2 - e1)) e1
wenzelm@63356
   608
     else Float (m2 + m1 * 2^nat (e1 - e2)) e2)"
lp15@65583
   609
  by transfer (simp add: field_simps powr_realpow[symmetric] powr_diff)
hoelzl@47599
   610
wenzelm@63356
   611
qualified lemma compute_float_minus[code]: "f - g = f + (-g)" for f g :: float
hoelzl@47600
   612
  by simp
hoelzl@47599
   613
wenzelm@63356
   614
qualified lemma compute_float_sgn[code]:
wenzelm@63356
   615
  "sgn (Float m1 e1) = (if 0 < m1 then 1 else if m1 < 0 then -1 else 0)"
haftmann@64240
   616
  by transfer (simp add: sgn_mult)
hoelzl@47599
   617
nipkow@67399
   618
lift_definition is_float_pos :: "float \<Rightarrow> bool" is "(<) 0 :: real \<Rightarrow> bool" .
hoelzl@47599
   619
wenzelm@60698
   620
qualified lemma compute_is_float_pos[code]: "is_float_pos (Float m e) \<longleftrightarrow> 0 < m"
hoelzl@47600
   621
  by transfer (auto simp add: zero_less_mult_iff not_le[symmetric, of _ 0])
hoelzl@47599
   622
nipkow@67399
   623
lift_definition is_float_nonneg :: "float \<Rightarrow> bool" is "(\<le>) 0 :: real \<Rightarrow> bool" .
hoelzl@47599
   624
wenzelm@60698
   625
qualified lemma compute_is_float_nonneg[code]: "is_float_nonneg (Float m e) \<longleftrightarrow> 0 \<le> m"
hoelzl@47600
   626
  by transfer (auto simp add: zero_le_mult_iff not_less[symmetric, of _ 0])
hoelzl@47599
   627
nipkow@67399
   628
lift_definition is_float_zero :: "float \<Rightarrow> bool"  is "(=) 0 :: real \<Rightarrow> bool" .
hoelzl@47599
   629
wenzelm@60698
   630
qualified lemma compute_is_float_zero[code]: "is_float_zero (Float m e) \<longleftrightarrow> 0 = m"
hoelzl@47600
   631
  by transfer (auto simp add: is_float_zero_def)
hoelzl@47599
   632
wenzelm@61945
   633
qualified lemma compute_float_abs[code]: "\<bar>Float m e\<bar> = Float \<bar>m\<bar> e"
hoelzl@47600
   634
  by transfer (simp add: abs_mult)
hoelzl@47599
   635
wenzelm@60698
   636
qualified lemma compute_float_eq[code]: "equal_class.equal f g = is_float_zero (f - g)"
hoelzl@47600
   637
  by transfer simp
wenzelm@60698
   638
wenzelm@60698
   639
end
hoelzl@47599
   640
immler@58982
   641
wenzelm@60500
   642
subsection \<open>Lemmas for types @{typ real}, @{typ nat}, @{typ int}\<close>
immler@58982
   643
immler@58982
   644
lemmas real_of_ints =
lp15@61609
   645
  of_int_add
lp15@61609
   646
  of_int_minus
lp15@61609
   647
  of_int_diff
lp15@61609
   648
  of_int_mult
lp15@61609
   649
  of_int_power
lp15@61609
   650
  of_int_numeral of_int_neg_numeral
immler@58982
   651
immler@58982
   652
lemmas int_of_reals = real_of_ints[symmetric]
immler@58982
   653
immler@58982
   654
wenzelm@60500
   655
subsection \<open>Rounding Real Numbers\<close>
hoelzl@47599
   656
wenzelm@60698
   657
definition round_down :: "int \<Rightarrow> real \<Rightarrow> real"
wenzelm@61942
   658
  where "round_down prec x = \<lfloor>x * 2 powr prec\<rfloor> * 2 powr -prec"
hoelzl@47599
   659
wenzelm@60698
   660
definition round_up :: "int \<Rightarrow> real \<Rightarrow> real"
wenzelm@61942
   661
  where "round_up prec x = \<lceil>x * 2 powr prec\<rceil> * 2 powr -prec"
hoelzl@47599
   662
hoelzl@47599
   663
lemma round_down_float[simp]: "round_down prec x \<in> float"
hoelzl@47599
   664
  unfolding round_down_def
lp15@61609
   665
  by (auto intro!: times_float simp: of_int_minus[symmetric] simp del: of_int_minus)
hoelzl@47599
   666
hoelzl@47599
   667
lemma round_up_float[simp]: "round_up prec x \<in> float"
hoelzl@47599
   668
  unfolding round_up_def
lp15@61609
   669
  by (auto intro!: times_float simp: of_int_minus[symmetric] simp del: of_int_minus)
hoelzl@47599
   670
hoelzl@47599
   671
lemma round_up: "x \<le> round_up prec x"
lp15@61609
   672
  by (simp add: powr_minus_divide le_divide_eq round_up_def ceiling_correct)
hoelzl@47599
   673
hoelzl@47599
   674
lemma round_down: "round_down prec x \<le> x"
hoelzl@47599
   675
  by (simp add: powr_minus_divide divide_le_eq round_down_def)
hoelzl@47599
   676
hoelzl@47599
   677
lemma round_up_0[simp]: "round_up p 0 = 0"
hoelzl@47599
   678
  unfolding round_up_def by simp
hoelzl@47599
   679
hoelzl@47599
   680
lemma round_down_0[simp]: "round_down p 0 = 0"
hoelzl@47599
   681
  unfolding round_down_def by simp
hoelzl@47599
   682
wenzelm@63356
   683
lemma round_up_diff_round_down: "round_up prec x - round_down prec x \<le> 2 powr -prec"
hoelzl@47599
   684
proof -
wenzelm@63356
   685
  have "round_up prec x - round_down prec x = (\<lceil>x * 2 powr prec\<rceil> - \<lfloor>x * 2 powr prec\<rfloor>) * 2 powr -prec"
hoelzl@47599
   686
    by (simp add: round_up_def round_down_def field_simps)
hoelzl@47599
   687
  also have "\<dots> \<le> 1 * 2 powr -prec"
hoelzl@47599
   688
    by (rule mult_mono)
wenzelm@63356
   689
      (auto simp del: of_int_diff simp: of_int_diff[symmetric] ceiling_diff_floor_le_1)
hoelzl@47599
   690
  finally show ?thesis by simp
hoelzl@29804
   691
qed
hoelzl@29804
   692
hoelzl@47599
   693
lemma round_down_shift: "round_down p (x * 2 powr k) = 2 powr k * round_down (p + k) x"
hoelzl@47599
   694
  unfolding round_down_def
lp15@65583
   695
  by (simp add: powr_add powr_mult field_simps powr_diff)
hoelzl@47599
   696
    (simp add: powr_add[symmetric])
hoelzl@29804
   697
hoelzl@47599
   698
lemma round_up_shift: "round_up p (x * 2 powr k) = 2 powr k * round_up (p + k) x"
hoelzl@47599
   699
  unfolding round_up_def
lp15@65583
   700
  by (simp add: powr_add powr_mult field_simps powr_diff)
hoelzl@47599
   701
    (simp add: powr_add[symmetric])
hoelzl@47599
   702
immler@58982
   703
lemma round_up_uminus_eq: "round_up p (-x) = - round_down p x"
immler@58982
   704
  and round_down_uminus_eq: "round_down p (-x) = - round_up p x"
immler@58982
   705
  by (auto simp: round_up_def round_down_def ceiling_def)
immler@58982
   706
immler@58982
   707
lemma round_up_mono: "x \<le> y \<Longrightarrow> round_up p x \<le> round_up p y"
immler@58982
   708
  by (auto intro!: ceiling_mono simp: round_up_def)
immler@58982
   709
immler@58982
   710
lemma round_up_le1:
immler@58982
   711
  assumes "x \<le> 1" "prec \<ge> 0"
immler@58982
   712
  shows "round_up prec x \<le> 1"
immler@58982
   713
proof -
lp15@61609
   714
  have "real_of_int \<lceil>x * 2 powr prec\<rceil> \<le> real_of_int \<lceil>2 powr real_of_int prec\<rceil>"
immler@58982
   715
    using assms by (auto intro!: ceiling_mono)
immler@58982
   716
  also have "\<dots> = 2 powr prec" using assms by (auto simp: powr_int intro!: exI[where x="2^nat prec"])
immler@58982
   717
  finally show ?thesis
immler@58982
   718
    by (simp add: round_up_def) (simp add: powr_minus inverse_eq_divide)
immler@58982
   719
qed
immler@58982
   720
immler@58982
   721
lemma round_up_less1:
immler@58982
   722
  assumes "x < 1 / 2" "p > 0"
immler@58982
   723
  shows "round_up p x < 1"
immler@58982
   724
proof -
immler@58982
   725
  have "x * 2 powr p < 1 / 2 * 2 powr p"
immler@58982
   726
    using assms by simp
wenzelm@60500
   727
  also have "\<dots> \<le> 2 powr p - 1" using \<open>p > 0\<close>
lp15@65583
   728
    by (auto simp: powr_diff powr_int field_simps self_le_power)
wenzelm@60500
   729
  finally show ?thesis using \<open>p > 0\<close>
lp15@61609
   730
    by (simp add: round_up_def field_simps powr_minus powr_int ceiling_less_iff)
immler@58982
   731
qed
immler@58982
   732
immler@58982
   733
lemma round_down_ge1:
immler@58982
   734
  assumes x: "x \<ge> 1"
immler@58982
   735
  assumes prec: "p \<ge> - log 2 x"
immler@58982
   736
  shows "1 \<le> round_down p x"
immler@58982
   737
proof cases
immler@58982
   738
  assume nonneg: "0 \<le> p"
lp15@61609
   739
  have "2 powr p = real_of_int \<lfloor>2 powr real_of_int p\<rfloor>"
immler@58985
   740
    using nonneg by (auto simp: powr_int)
lp15@61609
   741
  also have "\<dots> \<le> real_of_int \<lfloor>x * 2 powr p\<rfloor>"
immler@58985
   742
    using assms by (auto intro!: floor_mono)
immler@58985
   743
  finally show ?thesis
immler@58985
   744
    by (simp add: round_down_def) (simp add: powr_minus inverse_eq_divide)
immler@58982
   745
next
immler@58982
   746
  assume neg: "\<not> 0 \<le> p"
immler@58982
   747
  have "x = 2 powr (log 2 x)"
immler@58982
   748
    using x by simp
immler@58982
   749
  also have "2 powr (log 2 x) \<ge> 2 powr - p"
immler@58982
   750
    using prec by auto
immler@58982
   751
  finally have x_le: "x \<ge> 2 powr -p" .
immler@58982
   752
lp15@61609
   753
  from neg have "2 powr real_of_int p \<le> 2 powr 0"
immler@58982
   754
    by (intro powr_mono) auto
lp15@60017
   755
  also have "\<dots> \<le> \<lfloor>2 powr 0::real\<rfloor>" by simp
lp15@61609
   756
  also have "\<dots> \<le> \<lfloor>x * 2 powr (real_of_int p)\<rfloor>"
lp15@61609
   757
    unfolding of_int_le_iff
immler@58982
   758
    using x x_le by (intro floor_mono) (simp add: powr_minus_divide field_simps)
immler@58982
   759
  finally show ?thesis
immler@58982
   760
    using prec x
immler@58982
   761
    by (simp add: round_down_def powr_minus_divide pos_le_divide_eq)
immler@58982
   762
qed
immler@58982
   763
immler@58982
   764
lemma round_up_le0: "x \<le> 0 \<Longrightarrow> round_up p x \<le> 0"
immler@58982
   765
  unfolding round_up_def
immler@58982
   766
  by (auto simp: field_simps mult_le_0_iff zero_le_mult_iff)
immler@58982
   767
immler@58982
   768
wenzelm@60500
   769
subsection \<open>Rounding Floats\<close>
hoelzl@29804
   770
wenzelm@60698
   771
definition div_twopow :: "int \<Rightarrow> nat \<Rightarrow> int"
wenzelm@60698
   772
  where [simp]: "div_twopow x n = x div (2 ^ n)"
immler@58985
   773
wenzelm@60698
   774
definition mod_twopow :: "int \<Rightarrow> nat \<Rightarrow> int"
wenzelm@60698
   775
  where [simp]: "mod_twopow x n = x mod (2 ^ n)"
immler@58985
   776
immler@58985
   777
lemma compute_div_twopow[code]:
immler@58985
   778
  "div_twopow x n = (if x = 0 \<or> x = -1 \<or> n = 0 then x else div_twopow (x div 2) (n - 1))"
immler@58985
   779
  by (cases n) (auto simp: zdiv_zmult2_eq div_eq_minus1)
immler@58985
   780
immler@58985
   781
lemma compute_mod_twopow[code]:
immler@58985
   782
  "mod_twopow x n = (if n = 0 then 0 else x mod 2 + 2 * mod_twopow (x div 2) (n - 1))"
immler@58985
   783
  by (cases n) (auto simp: zmod_zmult2_eq)
immler@58985
   784
hoelzl@47600
   785
lift_definition float_up :: "int \<Rightarrow> float \<Rightarrow> float" is round_up by simp
hoelzl@47601
   786
declare float_up.rep_eq[simp]
hoelzl@29804
   787
wenzelm@60698
   788
lemma round_up_correct: "round_up e f - f \<in> {0..2 powr -e}"
wenzelm@60698
   789
  unfolding atLeastAtMost_iff
hoelzl@47599
   790
proof
wenzelm@60698
   791
  have "round_up e f - f \<le> round_up e f - round_down e f"
wenzelm@60698
   792
    using round_down by simp
wenzelm@60698
   793
  also have "\<dots> \<le> 2 powr -e"
wenzelm@60698
   794
    using round_up_diff_round_down by simp
lp15@61609
   795
  finally show "round_up e f - f \<le> 2 powr - (real_of_int e)"
hoelzl@47600
   796
    by simp
hoelzl@47600
   797
qed (simp add: algebra_simps round_up)
hoelzl@29804
   798
lp15@61609
   799
lemma float_up_correct: "real_of_float (float_up e f) - real_of_float f \<in> {0..2 powr -e}"
immler@54782
   800
  by transfer (rule round_up_correct)
immler@54782
   801
hoelzl@47600
   802
lift_definition float_down :: "int \<Rightarrow> float \<Rightarrow> float" is round_down by simp
hoelzl@47601
   803
declare float_down.rep_eq[simp]
obua@16782
   804
wenzelm@60698
   805
lemma round_down_correct: "f - (round_down e f) \<in> {0..2 powr -e}"
wenzelm@60698
   806
  unfolding atLeastAtMost_iff
hoelzl@47599
   807
proof
wenzelm@60698
   808
  have "f - round_down e f \<le> round_up e f - round_down e f"
wenzelm@60698
   809
    using round_up by simp
wenzelm@60698
   810
  also have "\<dots> \<le> 2 powr -e"
wenzelm@60698
   811
    using round_up_diff_round_down by simp
lp15@61609
   812
  finally show "f - round_down e f \<le> 2 powr - (real_of_int e)"
hoelzl@47600
   813
    by simp
hoelzl@47600
   814
qed (simp add: algebra_simps round_down)
obua@24301
   815
lp15@61609
   816
lemma float_down_correct: "real_of_float f - real_of_float (float_down e f) \<in> {0..2 powr -e}"
immler@54782
   817
  by transfer (rule round_down_correct)
immler@54782
   818
wenzelm@60698
   819
context
wenzelm@60698
   820
begin
wenzelm@60698
   821
wenzelm@60698
   822
qualified lemma compute_float_down[code]:
hoelzl@47599
   823
  "float_down p (Float m e) =
immler@58985
   824
    (if p + e < 0 then Float (div_twopow m (nat (-(p + e)))) (-p) else Float m e)"
wenzelm@60698
   825
proof (cases "p + e < 0")
wenzelm@60698
   826
  case True
lp15@61609
   827
  then have "real_of_int ((2::int) ^ nat (-(p + e))) = 2 powr (-(p + e))"
hoelzl@47599
   828
    using powr_realpow[of 2 "nat (-(p + e))"] by simp
wenzelm@60698
   829
  also have "\<dots> = 1 / 2 powr p / 2 powr e"
lp15@61609
   830
    unfolding powr_minus_divide of_int_minus by (simp add: powr_add)
hoelzl@47599
   831
  finally show ?thesis
wenzelm@60500
   832
    using \<open>p + e < 0\<close>
lp15@61609
   833
    apply transfer
wenzelm@63356
   834
    apply (simp add: ac_simps round_down_def floor_divide_of_int_eq[symmetric])
lp15@61609
   835
    proof - (*FIXME*)
lp15@61609
   836
      fix pa :: int and ea :: int and ma :: int
lp15@61609
   837
      assume a1: "2 ^ nat (- pa - ea) = 1 / (2 powr real_of_int pa * 2 powr real_of_int ea)"
lp15@61609
   838
      assume "pa + ea < 0"
wenzelm@63356
   839
      have "\<lfloor>real_of_int ma / real_of_int (int 2 ^ nat (- (pa + ea)))\<rfloor> =
wenzelm@63356
   840
          \<lfloor>real_of_float (Float ma (pa + ea))\<rfloor>"
lp15@61609
   841
        using a1 by (simp add: powr_add)
wenzelm@63356
   842
      then show "\<lfloor>real_of_int ma * (2 powr real_of_int pa * 2 powr real_of_int ea)\<rfloor> =
wenzelm@63356
   843
          ma div 2 ^ nat (- pa - ea)"
wenzelm@63356
   844
        by (metis Float.rep_eq add_uminus_conv_diff floor_divide_of_int_eq
wenzelm@63356
   845
            minus_add_distrib of_int_simps(3) of_nat_numeral powr_add)
lp15@61609
   846
    qed
hoelzl@47599
   847
next
wenzelm@60698
   848
  case False
wenzelm@63356
   849
  then have r: "real_of_int e + real_of_int p = real (nat (e + p))"
wenzelm@63356
   850
    by simp
lp15@61609
   851
  have r: "\<lfloor>(m * 2 powr e) * 2 powr real_of_int p\<rfloor> = (m * 2 powr e) * 2 powr real_of_int p"
hoelzl@47600
   852
    by (auto intro: exI[where x="m*2^nat (e+p)"]
wenzelm@63356
   853
        simp add: ac_simps powr_add[symmetric] r powr_realpow)
wenzelm@60500
   854
  with \<open>\<not> p + e < 0\<close> show ?thesis
wenzelm@57862
   855
    by transfer (auto simp add: round_down_def field_simps powr_add powr_minus)
hoelzl@47599
   856
qed
obua@24301
   857
immler@54782
   858
lemma abs_round_down_le: "\<bar>f - (round_down e f)\<bar> \<le> 2 powr -e"
immler@54782
   859
  using round_down_correct[of f e] by simp
immler@54782
   860
immler@54782
   861
lemma abs_round_up_le: "\<bar>f - (round_up e f)\<bar> \<le> 2 powr -e"
immler@54782
   862
  using round_up_correct[of e f] by simp
immler@54782
   863
immler@54782
   864
lemma round_down_nonneg: "0 \<le> s \<Longrightarrow> 0 \<le> round_down p s"
nipkow@56536
   865
  by (auto simp: round_down_def)
immler@54782
   866
hoelzl@47599
   867
lemma ceil_divide_floor_conv:
wenzelm@60698
   868
  assumes "b \<noteq> 0"
wenzelm@63356
   869
  shows "\<lceil>real_of_int a / real_of_int b\<rceil> =
wenzelm@63356
   870
    (if b dvd a then a div b else \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1)"
wenzelm@60698
   871
proof (cases "b dvd a")
wenzelm@60698
   872
  case True
wenzelm@60698
   873
  then show ?thesis
lp15@61609
   874
    by (simp add: ceiling_def of_int_minus[symmetric] divide_minus_left[symmetric]
lp15@61609
   875
      floor_divide_of_int_eq dvd_neg_div del: divide_minus_left of_int_minus)
wenzelm@60698
   876
next
wenzelm@60698
   877
  case False
wenzelm@60698
   878
  then have "a mod b \<noteq> 0"
wenzelm@60698
   879
    by auto
lp15@61609
   880
  then have ne: "real_of_int (a mod b) / real_of_int b \<noteq> 0"
wenzelm@60698
   881
    using \<open>b \<noteq> 0\<close> by auto
lp15@61609
   882
  have "\<lceil>real_of_int a / real_of_int b\<rceil> = \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1"
wenzelm@60698
   883
    apply (rule ceiling_eq)
lp15@61609
   884
    apply (auto simp: floor_divide_of_int_eq[symmetric])
hoelzl@47599
   885
  proof -
lp15@61609
   886
    have "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> \<le> real_of_int a / real_of_int b"
wenzelm@60698
   887
      by simp
lp15@61609
   888
    moreover have "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> \<noteq> real_of_int a / real_of_int b"
wenzelm@60698
   889
      apply (subst (2) real_of_int_div_aux)
lp15@61609
   890
      unfolding floor_divide_of_int_eq
wenzelm@60698
   891
      using ne \<open>b \<noteq> 0\<close> apply auto
wenzelm@60698
   892
      done
lp15@61609
   893
    ultimately show "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> < real_of_int a / real_of_int b" by arith
hoelzl@47599
   894
  qed
wenzelm@60698
   895
  then show ?thesis
wenzelm@60698
   896
    using \<open>\<not> b dvd a\<close> by simp
wenzelm@60698
   897
qed
wenzelm@19765
   898
wenzelm@60698
   899
qualified lemma compute_float_up[code]: "float_up p x = - float_down p (-x)"
immler@58982
   900
  by transfer (simp add: round_down_uminus_eq)
wenzelm@60698
   901
wenzelm@60698
   902
end
hoelzl@29804
   903
hoelzl@29804
   904
nipkow@63664
   905
lemma bitlen_Float:
nipkow@63664
   906
  fixes m e
immler@67573
   907
  defines [THEN meta_eq_to_obj_eq]: "f \<equiv> Float m e"
nipkow@63664
   908
  shows "bitlen \<bar>mantissa f\<bar> + exponent f = (if m = 0 then 0 else bitlen \<bar>m\<bar> + e)"
nipkow@63664
   909
proof (cases "m = 0")
nipkow@63664
   910
  case True
immler@67573
   911
  then show ?thesis by (simp add: f_def bitlen_alt_def)
nipkow@63664
   912
next
nipkow@63664
   913
  case False
immler@67573
   914
  then have "f \<noteq> 0"
nipkow@63664
   915
    unfolding real_of_float_eq by (simp add: f_def)
nipkow@63664
   916
  then have "mantissa f \<noteq> 0"
immler@67573
   917
    by (simp add: mantissa_eq_zero_iff)
nipkow@63664
   918
  moreover
nipkow@63664
   919
  obtain i where "m = mantissa f * 2 ^ i" "e = exponent f - int i"
immler@67573
   920
    by (rule f_def[THEN denormalize_shift, OF \<open>f \<noteq> 0\<close>])
nipkow@63664
   921
  ultimately show ?thesis by (simp add: abs_mult)
nipkow@63664
   922
qed
nipkow@63664
   923
wenzelm@63356
   924
lemma float_gt1_scale:
wenzelm@63356
   925
  assumes "1 \<le> Float m e"
hoelzl@47599
   926
  shows "0 \<le> e + (bitlen m - 1)"
hoelzl@47599
   927
proof -
hoelzl@47599
   928
  have "0 < Float m e" using assms by auto
wenzelm@60698
   929
  then have "0 < m" using powr_gt_zero[of 2 e]
immler@67573
   930
    by (auto simp: zero_less_mult_iff)
wenzelm@60698
   931
  then have "m \<noteq> 0" by auto
hoelzl@47599
   932
  show ?thesis
hoelzl@47599
   933
  proof (cases "0 \<le> e")
wenzelm@60698
   934
    case True
wenzelm@60698
   935
    then show ?thesis
immler@63248
   936
      using \<open>0 < m\<close> by (simp add: bitlen_alt_def)
hoelzl@29804
   937
  next
wenzelm@60698
   938
    case False
hoelzl@47599
   939
    have "(1::int) < 2" by simp
wenzelm@60698
   940
    let ?S = "2^(nat (-e))"
wenzelm@60698
   941
    have "inverse (2 ^ nat (- e)) = 2 powr e"
wenzelm@60698
   942
      using assms False powr_realpow[of 2 "nat (-e)"]
wenzelm@57862
   943
      by (auto simp: powr_minus field_simps)
lp15@61609
   944
    then have "1 \<le> real_of_int m * inverse ?S"
wenzelm@60698
   945
      using assms False powr_realpow[of 2 "nat (-e)"]
hoelzl@47599
   946
      by (auto simp: powr_minus)
lp15@61609
   947
    then have "1 * ?S \<le> real_of_int m * inverse ?S * ?S"
wenzelm@60698
   948
      by (rule mult_right_mono) auto
lp15@61609
   949
    then have "?S \<le> real_of_int m"
wenzelm@60698
   950
      unfolding mult.assoc by auto
wenzelm@60698
   951
    then have "?S \<le> m"
lp15@61609
   952
      unfolding of_int_le_iff[symmetric] by auto
wenzelm@60500
   953
    from this bitlen_bounds[OF \<open>0 < m\<close>, THEN conjunct2]
wenzelm@60698
   954
    have "nat (-e) < (nat (bitlen m))"
wenzelm@60698
   955
      unfolding power_strict_increasing_iff[OF \<open>1 < 2\<close>, symmetric]
immler@58985
   956
      by (rule order_le_less_trans)
wenzelm@60698
   957
    then have "-e < bitlen m"
wenzelm@60698
   958
      using False by auto
wenzelm@60698
   959
    then show ?thesis
wenzelm@60698
   960
      by auto
hoelzl@29804
   961
  qed
hoelzl@47599
   962
qed
hoelzl@29804
   963
wenzelm@60698
   964
wenzelm@60500
   965
subsection \<open>Truncating Real Numbers\<close>
immler@58985
   966
wenzelm@60698
   967
definition truncate_down::"nat \<Rightarrow> real \<Rightarrow> real"
immler@62420
   968
  where "truncate_down prec x = round_down (prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) x"
immler@58985
   969
immler@58985
   970
lemma truncate_down: "truncate_down prec x \<le> x"
immler@58985
   971
  using round_down by (simp add: truncate_down_def)
immler@58985
   972
immler@58985
   973
lemma truncate_down_le: "x \<le> y \<Longrightarrow> truncate_down prec x \<le> y"
immler@58985
   974
  by (rule order_trans[OF truncate_down])
immler@58985
   975
immler@58985
   976
lemma truncate_down_zero[simp]: "truncate_down prec 0 = 0"
immler@58985
   977
  by (simp add: truncate_down_def)
immler@58985
   978
immler@58985
   979
lemma truncate_down_float[simp]: "truncate_down p x \<in> float"
immler@58985
   980
  by (auto simp: truncate_down_def)
immler@58985
   981
wenzelm@60698
   982
definition truncate_up::"nat \<Rightarrow> real \<Rightarrow> real"
immler@62420
   983
  where "truncate_up prec x = round_up (prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) x"
immler@58985
   984
immler@58985
   985
lemma truncate_up: "x \<le> truncate_up prec x"
immler@58985
   986
  using round_up by (simp add: truncate_up_def)
immler@58985
   987
immler@58985
   988
lemma truncate_up_le: "x \<le> y \<Longrightarrow> x \<le> truncate_up prec y"
immler@58985
   989
  by (rule order_trans[OF _ truncate_up])
immler@58985
   990
immler@58985
   991
lemma truncate_up_zero[simp]: "truncate_up prec 0 = 0"
immler@58985
   992
  by (simp add: truncate_up_def)
immler@58985
   993
immler@58985
   994
lemma truncate_up_uminus_eq: "truncate_up prec (-x) = - truncate_down prec x"
immler@58985
   995
  and truncate_down_uminus_eq: "truncate_down prec (-x) = - truncate_up prec x"
immler@58985
   996
  by (auto simp: truncate_up_def round_up_def truncate_down_def round_down_def ceiling_def)
immler@58985
   997
immler@58985
   998
lemma truncate_up_float[simp]: "truncate_up p x \<in> float"
immler@58985
   999
  by (auto simp: truncate_up_def)
immler@58985
  1000
immler@58985
  1001
lemma mult_powr_eq: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> x * b powr y = b powr (y + log b x)"
immler@58985
  1002
  by (simp_all add: powr_add)
immler@58985
  1003
immler@58985
  1004
lemma truncate_down_pos:
immler@62420
  1005
  assumes "x > 0"
immler@58985
  1006
  shows "truncate_down p x > 0"
immler@58985
  1007
proof -
lp15@61609
  1008
  have "0 \<le> log 2 x - real_of_int \<lfloor>log 2 x\<rfloor>"
immler@58985
  1009
    by (simp add: algebra_simps)
lp15@61762
  1010
  with assms
immler@58985
  1011
  show ?thesis
wenzelm@63356
  1012
    apply (auto simp: truncate_down_def round_down_def mult_powr_eq
immler@58985
  1013
      intro!: ge_one_powr_ge_zero mult_pos_pos)
lp15@61762
  1014
    by linarith
immler@58985
  1015
qed
immler@58985
  1016
immler@58985
  1017
lemma truncate_down_nonneg: "0 \<le> y \<Longrightarrow> 0 \<le> truncate_down prec y"
immler@58985
  1018
  by (auto simp: truncate_down_def round_down_def)
immler@58985
  1019
immler@62420
  1020
lemma truncate_down_ge1: "1 \<le> x \<Longrightarrow> 1 \<le> truncate_down p x"
immler@62420
  1021
  apply (auto simp: truncate_down_def algebra_simps intro!: round_down_ge1)
immler@62420
  1022
  apply linarith
immler@62420
  1023
  done
immler@58985
  1024
immler@58985
  1025
lemma truncate_up_nonpos: "x \<le> 0 \<Longrightarrow> truncate_up prec x \<le> 0"
immler@58985
  1026
  by (auto simp: truncate_up_def round_up_def intro!: mult_nonpos_nonneg)
hoelzl@47599
  1027
immler@58985
  1028
lemma truncate_up_le1:
immler@62420
  1029
  assumes "x \<le> 1"
wenzelm@60698
  1030
  shows "truncate_up p x \<le> 1"
immler@58985
  1031
proof -
wenzelm@60698
  1032
  consider "x \<le> 0" | "x > 0"
wenzelm@60698
  1033
    by arith
wenzelm@60698
  1034
  then show ?thesis
wenzelm@60698
  1035
  proof cases
wenzelm@60698
  1036
    case 1
wenzelm@60698
  1037
    with truncate_up_nonpos[OF this, of p] show ?thesis
wenzelm@60698
  1038
      by simp
wenzelm@60698
  1039
  next
wenzelm@60698
  1040
    case 2
wenzelm@60698
  1041
    then have le: "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<le> 0"
immler@58985
  1042
      using assms by (auto simp: log_less_iff)
immler@62420
  1043
    from assms have "0 \<le> int p" by simp
immler@58985
  1044
    from add_mono[OF this le]
wenzelm@60698
  1045
    show ?thesis
wenzelm@60698
  1046
      using assms by (simp add: truncate_up_def round_up_le1 add_mono)
wenzelm@60698
  1047
  qed
immler@58985
  1048
qed
immler@58985
  1049
wenzelm@63356
  1050
lemma truncate_down_shift_int:
wenzelm@63356
  1051
  "truncate_down p (x * 2 powr real_of_int k) = truncate_down p x * 2 powr k"
immler@62420
  1052
  by (cases "x = 0")
wenzelm@63356
  1053
    (simp_all add: algebra_simps abs_mult log_mult truncate_down_def
wenzelm@63356
  1054
      round_down_shift[of _ _ k, simplified])
immler@62420
  1055
immler@62420
  1056
lemma truncate_down_shift_nat: "truncate_down p (x * 2 powr real k) = truncate_down p x * 2 powr k"
immler@62420
  1057
  by (metis of_int_of_nat_eq truncate_down_shift_int)
immler@62420
  1058
immler@62420
  1059
lemma truncate_up_shift_int: "truncate_up p (x * 2 powr real_of_int k) = truncate_up p x * 2 powr k"
immler@62420
  1060
  by (cases "x = 0")
wenzelm@63356
  1061
    (simp_all add: algebra_simps abs_mult log_mult truncate_up_def
wenzelm@63356
  1062
      round_up_shift[of _ _ k, simplified])
immler@62420
  1063
immler@62420
  1064
lemma truncate_up_shift_nat: "truncate_up p (x * 2 powr real k) = truncate_up p x * 2 powr k"
immler@62420
  1065
  by (metis of_int_of_nat_eq truncate_up_shift_int)
immler@62420
  1066
wenzelm@60698
  1067
wenzelm@60500
  1068
subsection \<open>Truncating Floats\<close>
immler@58985
  1069
immler@58985
  1070
lift_definition float_round_up :: "nat \<Rightarrow> float \<Rightarrow> float" is truncate_up
immler@58985
  1071
  by (simp add: truncate_up_def)
immler@58985
  1072
lp15@61609
  1073
lemma float_round_up: "real_of_float x \<le> real_of_float (float_round_up prec x)"
immler@58985
  1074
  using truncate_up by transfer simp
immler@58985
  1075
immler@58985
  1076
lemma float_round_up_zero[simp]: "float_round_up prec 0 = 0"
immler@58985
  1077
  by transfer simp
immler@58985
  1078
immler@58985
  1079
lift_definition float_round_down :: "nat \<Rightarrow> float \<Rightarrow> float" is truncate_down
immler@58985
  1080
  by (simp add: truncate_down_def)
immler@58985
  1081
lp15@61609
  1082
lemma float_round_down: "real_of_float (float_round_down prec x) \<le> real_of_float x"
immler@58985
  1083
  using truncate_down by transfer simp
immler@58985
  1084
immler@58985
  1085
lemma float_round_down_zero[simp]: "float_round_down prec 0 = 0"
immler@58985
  1086
  by transfer simp
immler@58985
  1087
immler@58985
  1088
lemmas float_round_up_le = order_trans[OF _ float_round_up]
immler@58985
  1089
  and float_round_down_le = order_trans[OF float_round_down]
immler@58985
  1090
immler@58985
  1091
lemma minus_float_round_up_eq: "- float_round_up prec x = float_round_down prec (- x)"
immler@58985
  1092
  and minus_float_round_down_eq: "- float_round_down prec x = float_round_up prec (- x)"
wenzelm@63356
  1093
  by (transfer; simp add: truncate_down_uminus_eq truncate_up_uminus_eq)+
immler@58985
  1094
wenzelm@60698
  1095
context
wenzelm@60698
  1096
begin
wenzelm@60698
  1097
wenzelm@60698
  1098
qualified lemma compute_float_round_down[code]:
wenzelm@63356
  1099
  "float_round_down prec (Float m e) =
wenzelm@63356
  1100
    (let d = bitlen \<bar>m\<bar> - int prec - 1 in
wenzelm@63356
  1101
      if 0 < d then Float (div_twopow m (nat d)) (e + d)
wenzelm@63356
  1102
      else Float m e)"
immler@62420
  1103
  using Float.compute_float_down[of "Suc prec - bitlen \<bar>m\<bar> - e" m e, symmetric]
immler@62420
  1104
  by transfer
immler@63248
  1105
    (simp add: field_simps abs_mult log_mult bitlen_alt_def truncate_down_def
immler@62420
  1106
      cong del: if_weak_cong)
immler@58985
  1107
wenzelm@60698
  1108
qualified lemma compute_float_round_up[code]:
immler@58985
  1109
  "float_round_up prec x = - float_round_down prec (-x)"
immler@58985
  1110
  by transfer (simp add: truncate_down_uminus_eq)
wenzelm@60698
  1111
wenzelm@60698
  1112
end
immler@58985
  1113
immler@58985
  1114
wenzelm@60500
  1115
subsection \<open>Approximation of positive rationals\<close>
hoelzl@29804
  1116
wenzelm@63356
  1117
lemma div_mult_twopow_eq: "a div ((2::nat) ^ n) div b = a div (b * 2 ^ n)" for a b :: nat
wenzelm@60698
  1118
  by (cases "b = 0") (simp_all add: div_mult2_eq[symmetric] ac_simps)
hoelzl@29804
  1119
wenzelm@63356
  1120
lemma real_div_nat_eq_floor_of_divide: "a div b = real_of_int \<lfloor>a / b\<rfloor>" for a b :: nat
lp15@61609
  1121
  by (simp add: floor_divide_of_nat_eq [of a b])
hoelzl@29804
  1122
immler@62420
  1123
definition "rat_precision prec x y =
wenzelm@63356
  1124
  (let d = bitlen x - bitlen y
wenzelm@63356
  1125
   in int prec - d + (if Float (abs x) 0 < Float (abs y) d then 1 else 0))"
immler@62420
  1126
immler@62420
  1127
lemma floor_log_divide_eq:
immler@62420
  1128
  assumes "i > 0" "j > 0" "p > 1"
immler@62420
  1129
  shows "\<lfloor>log p (i / j)\<rfloor> = floor (log p i) - floor (log p j) -
wenzelm@63356
  1130
    (if i \<ge> j * p powr (floor (log p i) - floor (log p j)) then 0 else 1)"
immler@62420
  1131
proof -
immler@62420
  1132
  let ?l = "log p"
immler@62420
  1133
  let ?fl = "\<lambda>x. floor (?l x)"
immler@62420
  1134
  have "\<lfloor>?l (i / j)\<rfloor> = \<lfloor>?l i - ?l j\<rfloor>" using assms
immler@62420
  1135
    by (auto simp: log_divide)
immler@62420
  1136
  also have "\<dots> = floor (real_of_int (?fl i - ?fl j) + (?l i - ?fl i - (?l j - ?fl j)))"
immler@62420
  1137
    (is "_ = floor (_ + ?r)")
immler@62420
  1138
    by (simp add: algebra_simps)
immler@62420
  1139
  also note floor_add2
immler@62420
  1140
  also note \<open>p > 1\<close>
immler@62420
  1141
  note powr = powr_le_cancel_iff[symmetric, OF \<open>1 < p\<close>, THEN iffD2]
immler@62420
  1142
  note powr_strict = powr_less_cancel_iff[symmetric, OF \<open>1 < p\<close>, THEN iffD2]
immler@62420
  1143
  have "floor ?r = (if i \<ge> j * p powr (?fl i - ?fl j) then 0 else -1)" (is "_ = ?if")
immler@62420
  1144
    using assms
immler@62420
  1145
    by (linarith |
immler@62420
  1146
      auto
immler@62420
  1147
        intro!: floor_eq2
immler@62420
  1148
        intro: powr_strict powr
lp15@65583
  1149
        simp: powr_diff powr_add divide_simps algebra_simps)+
immler@62420
  1150
  finally
immler@62420
  1151
  show ?thesis by simp
immler@62420
  1152
qed
immler@62420
  1153
immler@62420
  1154
lemma truncate_down_rat_precision:
immler@62420
  1155
    "truncate_down prec (real x / real y) = round_down (rat_precision prec x y) (real x / real y)"
immler@62420
  1156
  and truncate_up_rat_precision:
immler@62420
  1157
    "truncate_up prec (real x / real y) = round_up (rat_precision prec x y) (real x / real y)"
immler@62420
  1158
  unfolding truncate_down_def truncate_up_def rat_precision_def
immler@63248
  1159
  by (cases x; cases y) (auto simp: floor_log_divide_eq algebra_simps bitlen_alt_def)
hoelzl@29804
  1160
hoelzl@47600
  1161
lift_definition lapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float"
immler@62420
  1162
  is "\<lambda>prec (x::nat) (y::nat). truncate_down prec (x / y)"
wenzelm@60698
  1163
  by simp
obua@16782
  1164
wenzelm@60698
  1165
context
wenzelm@60698
  1166
begin
wenzelm@60698
  1167
wenzelm@60698
  1168
qualified lemma compute_lapprox_posrat[code]:
wenzelm@63356
  1169
  "lapprox_posrat prec x y =
wenzelm@53381
  1170
   (let
wenzelm@60698
  1171
      l = rat_precision prec x y;
wenzelm@60698
  1172
      d = if 0 \<le> l then x * 2^nat l div y else x div 2^nat (- l) div y
hoelzl@47599
  1173
    in normfloat (Float d (- l)))"
immler@58982
  1174
    unfolding div_mult_twopow_eq
hoelzl@47600
  1175
    by transfer
immler@62420
  1176
      (simp add: round_down_def powr_int real_div_nat_eq_floor_of_divide field_simps Let_def
immler@62420
  1177
        truncate_down_rat_precision del: two_powr_minus_int_float)
wenzelm@60698
  1178
wenzelm@60698
  1179
end
hoelzl@29804
  1180
hoelzl@47600
  1181
lift_definition rapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float"
immler@62420
  1182
  is "\<lambda>prec (x::nat) (y::nat). truncate_up prec (x / y)"
immler@62420
  1183
  by simp
hoelzl@29804
  1184
wenzelm@60376
  1185
context
wenzelm@60376
  1186
begin
wenzelm@60376
  1187
wenzelm@60376
  1188
qualified lemma compute_rapprox_posrat[code]:
hoelzl@47599
  1189
  fixes prec x y
hoelzl@47599
  1190
  defines "l \<equiv> rat_precision prec x y"
wenzelm@63356
  1191
  shows "rapprox_posrat prec x y =
wenzelm@63356
  1192
   (let
wenzelm@63356
  1193
      l = l;
wenzelm@63356
  1194
      (r, s) = if 0 \<le> l then (x * 2^nat l, y) else (x, y * 2^nat(-l));
wenzelm@63356
  1195
      d = r div s;
wenzelm@63356
  1196
      m = r mod s
wenzelm@63356
  1197
    in normfloat (Float (d + (if m = 0 \<or> y = 0 then 0 else 1)) (- l)))"
hoelzl@47599
  1198
proof (cases "y = 0")
wenzelm@60698
  1199
  assume "y = 0"
wenzelm@60698
  1200
  then show ?thesis by transfer simp
hoelzl@47599
  1201
next
hoelzl@47599
  1202
  assume "y \<noteq> 0"
hoelzl@29804
  1203
  show ?thesis
hoelzl@47599
  1204
  proof (cases "0 \<le> l")
wenzelm@60698
  1205
    case True
wenzelm@63040
  1206
    define x' where "x' = x * 2 ^ nat l"
wenzelm@60698
  1207
    have "int x * 2 ^ nat l = x'"
wenzelm@63356
  1208
      by (simp add: x'_def)
lp15@61609
  1209
    moreover have "real x * 2 powr l = real x'"
wenzelm@60500
  1210
      by (simp add: powr_realpow[symmetric] \<open>0 \<le> l\<close> x'_def)
hoelzl@47599
  1211
    ultimately show ?thesis
wenzelm@60500
  1212
      using ceil_divide_floor_conv[of y x'] powr_realpow[of 2 "nat l"] \<open>0 \<le> l\<close> \<open>y \<noteq> 0\<close>
hoelzl@47600
  1213
        l_def[symmetric, THEN meta_eq_to_obj_eq]
lp15@61609
  1214
      apply transfer
immler@62420
  1215
      apply (auto simp add: round_up_def truncate_up_rat_precision)
haftmann@67118
  1216
      apply (metis dvd_triv_left of_nat_dvd_iff)
wenzelm@63356
  1217
      apply (metis floor_divide_of_int_eq of_int_of_nat_eq)
wenzelm@63356
  1218
      done
hoelzl@47599
  1219
   next
wenzelm@60698
  1220
    case False
wenzelm@63040
  1221
    define y' where "y' = y * 2 ^ nat (- l)"
wenzelm@60500
  1222
    from \<open>y \<noteq> 0\<close> have "y' \<noteq> 0" by (simp add: y'_def)
wenzelm@63356
  1223
    have "int y * 2 ^ nat (- l) = y'"
wenzelm@63356
  1224
      by (simp add: y'_def)
lp15@61609
  1225
    moreover have "real x * real_of_int (2::int) powr real_of_int l / real y = x / real y'"
wenzelm@63356
  1226
      using \<open>\<not> 0 \<le> l\<close> by (simp add: powr_realpow[symmetric] powr_minus y'_def field_simps)
hoelzl@47599
  1227
    ultimately show ?thesis
wenzelm@60500
  1228
      using ceil_divide_floor_conv[of y' x] \<open>\<not> 0 \<le> l\<close> \<open>y' \<noteq> 0\<close> \<open>y \<noteq> 0\<close>
hoelzl@47600
  1229
        l_def[symmetric, THEN meta_eq_to_obj_eq]
lp15@61609
  1230
      apply transfer
immler@62420
  1231
      apply (auto simp add: round_up_def ceil_divide_floor_conv truncate_up_rat_precision)
haftmann@67118
  1232
      apply (metis dvd_triv_left of_nat_dvd_iff)
wenzelm@63356
  1233
      apply (metis floor_divide_of_int_eq of_int_of_nat_eq)
wenzelm@63356
  1234
      done
hoelzl@29804
  1235
  qed
hoelzl@29804
  1236
qed
wenzelm@60376
  1237
wenzelm@60376
  1238
end
hoelzl@29804
  1239
hoelzl@47599
  1240
lemma rat_precision_pos:
wenzelm@60698
  1241
  assumes "0 \<le> x"
wenzelm@60698
  1242
    and "0 < y"
wenzelm@60698
  1243
    and "2 * x < y"
hoelzl@47599
  1244
  shows "rat_precision n (int x) (int y) > 0"
hoelzl@29804
  1245
proof -
wenzelm@60698
  1246
  have "0 < x \<Longrightarrow> log 2 x + 1 = log 2 (2 * x)"
wenzelm@60698
  1247
    by (simp add: log_mult)
wenzelm@60698
  1248
  then have "bitlen (int x) < bitlen (int y)"
wenzelm@60698
  1249
    using assms
nipkow@63599
  1250
    by (simp add: bitlen_alt_def)
nipkow@63599
  1251
      (auto intro!: floor_mono simp add: one_add_floor)
wenzelm@60698
  1252
  then show ?thesis
wenzelm@60698
  1253
    using assms
wenzelm@60698
  1254
    by (auto intro!: pos_add_strict simp add: field_simps rat_precision_def)
hoelzl@29804
  1255
qed
obua@16782
  1256
hoelzl@47601
  1257
lemma rapprox_posrat_less1:
immler@62420
  1258
  "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> 2 * x < y \<Longrightarrow> real_of_float (rapprox_posrat n x y) < 1"
immler@62420
  1259
  by transfer (simp add: rat_precision_pos round_up_less1 truncate_up_rat_precision)
hoelzl@29804
  1260
hoelzl@47600
  1261
lift_definition lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" is
immler@62420
  1262
  "\<lambda>prec (x::int) (y::int). truncate_down prec (x / y)"
wenzelm@60698
  1263
  by simp
obua@16782
  1264
wenzelm@60698
  1265
context
wenzelm@60698
  1266
begin
wenzelm@60698
  1267
wenzelm@60698
  1268
qualified lemma compute_lapprox_rat[code]:
hoelzl@47599
  1269
  "lapprox_rat prec x y =
wenzelm@60698
  1270
   (if y = 0 then 0
hoelzl@47599
  1271
    else if 0 \<le> x then
wenzelm@60698
  1272
     (if 0 < y then lapprox_posrat prec (nat x) (nat y)
wenzelm@53381
  1273
      else - (rapprox_posrat prec (nat x) (nat (-y))))
wenzelm@63356
  1274
      else
wenzelm@63356
  1275
       (if 0 < y
hoelzl@47599
  1276
        then - (rapprox_posrat prec (nat (-x)) (nat y))
hoelzl@47599
  1277
        else lapprox_posrat prec (nat (-x)) (nat (-y))))"
immler@62420
  1278
  by transfer (simp add: truncate_up_uminus_eq)
hoelzl@47599
  1279
hoelzl@47600
  1280
lift_definition rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" is
immler@62420
  1281
  "\<lambda>prec (x::int) (y::int). truncate_up prec (x / y)"
wenzelm@60698
  1282
  by simp
hoelzl@47599
  1283
immler@58982
  1284
lemma "rapprox_rat = rapprox_posrat"
immler@58982
  1285
  by transfer auto
immler@58982
  1286
immler@58982
  1287
lemma "lapprox_rat = lapprox_posrat"
immler@58982
  1288
  by transfer auto
immler@58982
  1289
wenzelm@60698
  1290
qualified lemma compute_rapprox_rat[code]:
immler@58982
  1291
  "rapprox_rat prec x y = - lapprox_rat prec (-x) y"
immler@62420
  1292
  by transfer (simp add: truncate_down_uminus_eq)
immler@62420
  1293
wenzelm@63356
  1294
qualified lemma compute_truncate_down[code]:
wenzelm@63356
  1295
  "truncate_down p (Ratreal r) = (let (a, b) = quotient_of r in lapprox_rat p a b)"
immler@62420
  1296
  by transfer (auto split: prod.split simp: of_rat_divide dest!: quotient_of_div)
immler@62420
  1297
wenzelm@63356
  1298
qualified lemma compute_truncate_up[code]:
wenzelm@63356
  1299
  "truncate_up p (Ratreal r) = (let (a, b) = quotient_of r in rapprox_rat p a b)"
immler@62420
  1300
  by transfer (auto split: prod.split simp:  of_rat_divide dest!: quotient_of_div)
wenzelm@60698
  1301
wenzelm@60698
  1302
end
wenzelm@60698
  1303
hoelzl@47599
  1304
wenzelm@60500
  1305
subsection \<open>Division\<close>
hoelzl@47599
  1306
immler@62420
  1307
definition "real_divl prec a b = truncate_down prec (a / b)"
immler@54782
  1308
immler@62420
  1309
definition "real_divr prec a b = truncate_up prec (a / b)"
immler@54782
  1310
immler@54782
  1311
lift_definition float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divl
immler@54782
  1312
  by (simp add: real_divl_def)
hoelzl@47599
  1313
wenzelm@60698
  1314
context
wenzelm@60698
  1315
begin
wenzelm@60698
  1316
wenzelm@60698
  1317
qualified lemma compute_float_divl[code]:
hoelzl@47600
  1318
  "float_divl prec (Float m1 s1) (Float m2 s2) = lapprox_rat prec m1 m2 * Float 1 (s1 - s2)"
immler@62420
  1319
  apply transfer
immler@62420
  1320
  unfolding real_divl_def of_int_1 mult_1 truncate_down_shift_int[symmetric]
lp15@65583
  1321
  apply (simp add: powr_diff powr_minus)
immler@62420
  1322
  done
hoelzl@47600
  1323
immler@54782
  1324
lift_definition float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divr
immler@54782
  1325
  by (simp add: real_divr_def)
hoelzl@47599
  1326
wenzelm@60698
  1327
qualified lemma compute_float_divr[code]:
immler@58982
  1328
  "float_divr prec x y = - float_divl prec (-x) y"
immler@62420
  1329
  by transfer (simp add: real_divr_def real_divl_def truncate_down_uminus_eq)
wenzelm@60698
  1330
wenzelm@60698
  1331
end
hoelzl@47600
  1332
obua@16782
  1333
wenzelm@60500
  1334
subsection \<open>Approximate Power\<close>
immler@58985
  1335
wenzelm@63356
  1336
lemma div2_less_self[termination_simp]: "odd n \<Longrightarrow> n div 2 < n" for n :: nat
immler@58985
  1337
  by (simp add: odd_pos)
immler@58985
  1338
wenzelm@60698
  1339
fun power_down :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real"
wenzelm@60698
  1340
where
immler@58985
  1341
  "power_down p x 0 = 1"
immler@58985
  1342
| "power_down p x (Suc n) =
wenzelm@60698
  1343
    (if odd n then truncate_down (Suc p) ((power_down p x (Suc n div 2))\<^sup>2)
wenzelm@60698
  1344
     else truncate_down (Suc p) (x * power_down p x n))"
immler@58985
  1345
wenzelm@60698
  1346
fun power_up :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real"
wenzelm@60698
  1347
where
immler@58985
  1348
  "power_up p x 0 = 1"
immler@58985
  1349
| "power_up p x (Suc n) =
wenzelm@60698
  1350
    (if odd n then truncate_up p ((power_up p x (Suc n div 2))\<^sup>2)
wenzelm@60698
  1351
     else truncate_up p (x * power_up p x n))"
immler@58985
  1352
immler@58985
  1353
lift_definition power_up_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_up
immler@58985
  1354
  by (induct_tac rule: power_up.induct) simp_all
immler@58985
  1355
immler@58985
  1356
lift_definition power_down_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_down
immler@58985
  1357
  by (induct_tac rule: power_down.induct) simp_all
immler@58985
  1358
immler@58985
  1359
lemma power_float_transfer[transfer_rule]:
nipkow@67399
  1360
  "(rel_fun pcr_float (rel_fun (=) pcr_float)) (^) (^)"
immler@58985
  1361
  unfolding power_def
immler@58985
  1362
  by transfer_prover
immler@58985
  1363
immler@58985
  1364
lemma compute_power_up_fl[code]:
wenzelm@63356
  1365
    "power_up_fl p x 0 = 1"
wenzelm@63356
  1366
    "power_up_fl p x (Suc n) =
wenzelm@63356
  1367
      (if odd n then float_round_up p ((power_up_fl p x (Suc n div 2))\<^sup>2)
wenzelm@63356
  1368
       else float_round_up p (x * power_up_fl p x n))"
immler@58985
  1369
  and compute_power_down_fl[code]:
wenzelm@63356
  1370
    "power_down_fl p x 0 = 1"
wenzelm@63356
  1371
    "power_down_fl p x (Suc n) =
wenzelm@63356
  1372
      (if odd n then float_round_down (Suc p) ((power_down_fl p x (Suc n div 2))\<^sup>2)
wenzelm@63356
  1373
       else float_round_down (Suc p) (x * power_down_fl p x n))"
wenzelm@63356
  1374
  unfolding atomize_conj by transfer simp
immler@58985
  1375
immler@58985
  1376
lemma power_down_pos: "0 < x \<Longrightarrow> 0 < power_down p x n"
immler@58985
  1377
  by (induct p x n rule: power_down.induct)
immler@58985
  1378
    (auto simp del: odd_Suc_div_two intro!: truncate_down_pos)
immler@58985
  1379
immler@58985
  1380
lemma power_down_nonneg: "0 \<le> x \<Longrightarrow> 0 \<le> power_down p x n"
immler@58985
  1381
  by (induct p x n rule: power_down.induct)
immler@58985
  1382
    (auto simp del: odd_Suc_div_two intro!: truncate_down_nonneg mult_nonneg_nonneg)
immler@58985
  1383
immler@58985
  1384
lemma power_down: "0 \<le> x \<Longrightarrow> power_down p x n \<le> x ^ n"
immler@58985
  1385
proof (induct p x n rule: power_down.induct)
immler@58985
  1386
  case (2 p x n)
wenzelm@63356
  1387
  have ?case if "odd n"
wenzelm@63356
  1388
  proof -
wenzelm@63356
  1389
    from that 2 have "(power_down p x (Suc n div 2)) ^ 2 \<le> (x ^ (Suc n div 2)) ^ 2"
immler@58985
  1390
      by (auto intro: power_mono power_down_nonneg simp del: odd_Suc_div_two)
immler@58985
  1391
    also have "\<dots> = x ^ (Suc n div 2 * 2)"
immler@58985
  1392
      by (simp add: power_mult[symmetric])
immler@58985
  1393
    also have "Suc n div 2 * 2 = Suc n"
wenzelm@60500
  1394
      using \<open>odd n\<close> by presburger
wenzelm@63356
  1395
    finally show ?thesis
wenzelm@63356
  1396
      using that by (auto intro!: truncate_down_le simp del: odd_Suc_div_two)
wenzelm@63356
  1397
  qed
wenzelm@60698
  1398
  then show ?case
immler@58985
  1399
    by (auto intro!: truncate_down_le mult_left_mono 2 mult_nonneg_nonneg power_down_nonneg)
immler@58985
  1400
qed simp
immler@58985
  1401
immler@58985
  1402
lemma power_up: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up p x n"
immler@58985
  1403
proof (induct p x n rule: power_up.induct)
immler@58985
  1404
  case (2 p x n)
wenzelm@63356
  1405
  have ?case if "odd n"
wenzelm@63356
  1406
  proof -
wenzelm@63356
  1407
    from that even_Suc have "Suc n = Suc n div 2 * 2"
wenzelm@63356
  1408
      by presburger
wenzelm@60698
  1409
    then have "x ^ Suc n \<le> (x ^ (Suc n div 2))\<^sup>2"
immler@58985
  1410
      by (simp add: power_mult[symmetric])
wenzelm@63356
  1411
    also from that 2 have "\<dots> \<le> (power_up p x (Suc n div 2))\<^sup>2"
wenzelm@63356
  1412
      by (auto intro: power_mono simp del: odd_Suc_div_two)
wenzelm@63356
  1413
    finally show ?thesis
wenzelm@63356
  1414
      using that by (auto intro!: truncate_up_le simp del: odd_Suc_div_two)
wenzelm@63356
  1415
  qed
wenzelm@60698
  1416
  then show ?case
immler@58985
  1417
    by (auto intro!: truncate_up_le mult_left_mono 2)
immler@58985
  1418
qed simp
immler@58985
  1419
immler@58985
  1420
lemmas power_up_le = order_trans[OF _ power_up]
immler@58985
  1421
  and power_up_less = less_le_trans[OF _ power_up]
immler@58985
  1422
  and power_down_le = order_trans[OF power_down]
immler@58985
  1423
immler@58985
  1424
lemma power_down_fl: "0 \<le> x \<Longrightarrow> power_down_fl p x n \<le> x ^ n"
immler@58985
  1425
  by transfer (rule power_down)
immler@58985
  1426
immler@58985
  1427
lemma power_up_fl: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up_fl p x n"
immler@58985
  1428
  by transfer (rule power_up)
immler@58985
  1429
lp15@61609
  1430
lemma real_power_up_fl: "real_of_float (power_up_fl p x n) = power_up p x n"
immler@58985
  1431
  by transfer simp
immler@58985
  1432
lp15@61609
  1433
lemma real_power_down_fl: "real_of_float (power_down_fl p x n) = power_down p x n"
immler@58985
  1434
  by transfer simp
immler@58985
  1435
immler@58985
  1436
wenzelm@60500
  1437
subsection \<open>Approximate Addition\<close>
immler@58985
  1438
immler@58985
  1439
definition "plus_down prec x y = truncate_down prec (x + y)"
immler@58985
  1440
immler@58985
  1441
definition "plus_up prec x y = truncate_up prec (x + y)"
immler@58985
  1442
immler@58985
  1443
lemma float_plus_down_float[intro, simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> plus_down p x y \<in> float"
immler@58985
  1444
  by (simp add: plus_down_def)
immler@58985
  1445
immler@58985
  1446
lemma float_plus_up_float[intro, simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> plus_up p x y \<in> float"
immler@58985
  1447
  by (simp add: plus_up_def)
immler@58985
  1448
wenzelm@63356
  1449
lift_definition float_plus_down :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is plus_down ..
immler@58985
  1450
wenzelm@63356
  1451
lift_definition float_plus_up :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is plus_up ..
immler@58985
  1452
immler@58985
  1453
lemma plus_down: "plus_down prec x y \<le> x + y"
immler@58985
  1454
  and plus_up: "x + y \<le> plus_up prec x y"
immler@58985
  1455
  by (auto simp: plus_down_def truncate_down plus_up_def truncate_up)
immler@58985
  1456
lp15@61609
  1457
lemma float_plus_down: "real_of_float (float_plus_down prec x y) \<le> x + y"
lp15@61609
  1458
  and float_plus_up: "x + y \<le> real_of_float (float_plus_up prec x y)"
wenzelm@63356
  1459
  by (transfer; rule plus_down plus_up)+
immler@58985
  1460
immler@58985
  1461
lemmas plus_down_le = order_trans[OF plus_down]
immler@58985
  1462
  and plus_up_le = order_trans[OF _ plus_up]
immler@58985
  1463
  and float_plus_down_le = order_trans[OF float_plus_down]
immler@58985
  1464
  and float_plus_up_le = order_trans[OF _ float_plus_up]
immler@58985
  1465
immler@58985
  1466
lemma compute_plus_up[code]: "plus_up p x y = - plus_down p (-x) (-y)"
immler@58985
  1467
  using truncate_down_uminus_eq[of p "x + y"]
immler@58985
  1468
  by (auto simp: plus_down_def plus_up_def)
immler@58985
  1469
wenzelm@60698
  1470
lemma truncate_down_log2_eqI:
immler@58985
  1471
  assumes "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> = \<lfloor>log 2 \<bar>y\<bar>\<rfloor>"
immler@62420
  1472
  assumes "\<lfloor>x * 2 powr (p - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)\<rfloor> = \<lfloor>y * 2 powr (p - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)\<rfloor>"
immler@58985
  1473
  shows "truncate_down p x = truncate_down p y"
immler@58985
  1474
  using assms by (auto simp: truncate_down_def round_down_def)
immler@58985
  1475
wenzelm@60698
  1476
lemma sum_neq_zeroI:
wenzelm@63356
  1477
  "\<bar>a\<bar> \<ge> k \<Longrightarrow> \<bar>b\<bar> < k \<Longrightarrow> a + b \<noteq> 0"
wenzelm@63356
  1478
  "\<bar>a\<bar> > k \<Longrightarrow> \<bar>b\<bar> \<le> k \<Longrightarrow> a + b \<noteq> 0"
wenzelm@63356
  1479
  for a k :: real
immler@58985
  1480
  by auto
immler@58985
  1481
lp15@61609
  1482
lemma abs_real_le_2_powr_bitlen[simp]: "\<bar>real_of_int m2\<bar> < 2 powr real_of_int (bitlen \<bar>m2\<bar>)"
wenzelm@60698
  1483
proof (cases "m2 = 0")
wenzelm@60698
  1484
  case True
wenzelm@60698
  1485
  then show ?thesis by simp
wenzelm@60698
  1486
next
wenzelm@60698
  1487
  case False
wenzelm@60698
  1488
  then have "\<bar>m2\<bar> < 2 ^ nat (bitlen \<bar>m2\<bar>)"
immler@58985
  1489
    using bitlen_bounds[of "\<bar>m2\<bar>"]
immler@58985
  1490
    by (auto simp: powr_add bitlen_nonneg)
wenzelm@60698
  1491
  then show ?thesis
immler@66912
  1492
    by (metis bitlen_nonneg powr_int of_int_abs of_int_less_numeral_power_cancel_iff
immler@66912
  1493
        zero_less_numeral)
wenzelm@60698
  1494
qed
immler@58985
  1495
immler@58985
  1496
lemma floor_sum_times_2_powr_sgn_eq:
wenzelm@60698
  1497
  fixes ai p q :: int
wenzelm@60698
  1498
    and a b :: real
immler@58985
  1499
  assumes "a * 2 powr p = ai"
wenzelm@61945
  1500
    and b_le_1: "\<bar>b * 2 powr (p + 1)\<bar> \<le> 1"
wenzelm@60698
  1501
    and leqp: "q \<le> p"
immler@58985
  1502
  shows "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(2 * ai + sgn b) * 2 powr (q - p - 1)\<rfloor>"
immler@58985
  1503
proof -
wenzelm@60698
  1504
  consider "b = 0" | "b > 0" | "b < 0" by arith
wenzelm@60698
  1505
  then show ?thesis
wenzelm@60698
  1506
  proof cases
wenzelm@60698
  1507
    case 1
wenzelm@60698
  1508
    then show ?thesis
immler@58985
  1509
      by (simp add: assms(1)[symmetric] powr_add[symmetric] algebra_simps powr_mult_base)
wenzelm@60698
  1510
  next
wenzelm@60698
  1511
    case 2
wenzelm@61945
  1512
    then have "b * 2 powr p < \<bar>b * 2 powr (p + 1)\<bar>"
wenzelm@60698
  1513
      by simp
immler@58985
  1514
    also note b_le_1
lp15@61609
  1515
    finally have b_less_1: "b * 2 powr real_of_int p < 1" .
immler@58985
  1516
lp15@61609
  1517
    from b_less_1 \<open>b > 0\<close> have floor_eq: "\<lfloor>b * 2 powr real_of_int p\<rfloor> = 0" "\<lfloor>sgn b / 2\<rfloor> = 0"
immler@58985
  1518
      by (simp_all add: floor_eq_iff)
immler@58985
  1519
immler@58985
  1520
    have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(a + b) * 2 powr p * 2 powr (q - p)\<rfloor>"
immler@58985
  1521
      by (simp add: algebra_simps powr_realpow[symmetric] powr_add[symmetric])
immler@58985
  1522
    also have "\<dots> = \<lfloor>(ai + b * 2 powr p) * 2 powr (q - p)\<rfloor>"
immler@58985
  1523
      by (simp add: assms algebra_simps)
lp15@61609
  1524
    also have "\<dots> = \<lfloor>(ai + b * 2 powr p) / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
immler@58985
  1525
      using assms
immler@58985
  1526
      by (simp add: algebra_simps powr_realpow[symmetric] divide_powr_uminus powr_add[symmetric])
lp15@61609
  1527
    also have "\<dots> = \<lfloor>ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
lp15@61609
  1528
      by (simp del: of_int_power add: floor_divide_real_eq_div floor_eq)
lp15@61609
  1529
    finally have "\<lfloor>(a + b) * 2 powr real_of_int q\<rfloor> = \<lfloor>real_of_int ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>" .
immler@58985
  1530
    moreover
wenzelm@63356
  1531
    have "\<lfloor>(2 * ai + (sgn b)) * 2 powr (real_of_int (q - p) - 1)\<rfloor> =
wenzelm@63356
  1532
        \<lfloor>real_of_int ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
wenzelm@63356
  1533
    proof -
lp15@61609
  1534
      have "\<lfloor>(2 * ai + sgn b) * 2 powr (real_of_int (q - p) - 1)\<rfloor> = \<lfloor>(ai + sgn b / 2) * 2 powr (q - p)\<rfloor>"
lp15@65583
  1535
        by (subst powr_diff) (simp add: field_simps)
lp15@61609
  1536
      also have "\<dots> = \<lfloor>(ai + sgn b / 2) / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
lp15@65583
  1537
        using leqp by (simp add: powr_realpow[symmetric] powr_diff)
lp15@61609
  1538
      also have "\<dots> = \<lfloor>ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
lp15@61609
  1539
        by (simp del: of_int_power add: floor_divide_real_eq_div floor_eq)
wenzelm@63356
  1540
      finally show ?thesis .
wenzelm@63356
  1541
    qed
wenzelm@60698
  1542
    ultimately show ?thesis by simp
wenzelm@60698
  1543
  next
wenzelm@60698
  1544
    case 3
lp15@61609
  1545
    then have floor_eq: "\<lfloor>b * 2 powr (real_of_int p + 1)\<rfloor> = -1"
immler@58985
  1546
      using b_le_1
immler@58985
  1547
      by (auto simp: floor_eq_iff algebra_simps pos_divide_le_eq[symmetric] abs_if divide_powr_uminus
nipkow@62390
  1548
        intro!: mult_neg_pos split: if_split_asm)
immler@58985
  1549
    have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(2*a + 2*b) * 2 powr p * 2 powr (q - p - 1)\<rfloor>"
immler@58985
  1550
      by (simp add: algebra_simps powr_realpow[symmetric] powr_add[symmetric] powr_mult_base)
immler@58985
  1551
    also have "\<dots> = \<lfloor>(2 * (a * 2 powr p) + 2 * b * 2 powr p) * 2 powr (q - p - 1)\<rfloor>"
immler@58985
  1552
      by (simp add: algebra_simps)
immler@58985
  1553
    also have "\<dots> = \<lfloor>(2 * ai + b * 2 powr (p + 1)) / 2 powr (1 - q + p)\<rfloor>"
immler@58985
  1554
      using assms by (simp add: algebra_simps powr_mult_base divide_powr_uminus)
lp15@61609
  1555
    also have "\<dots> = \<lfloor>(2 * ai + b * 2 powr (p + 1)) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>"
immler@58985
  1556
      using assms by (simp add: algebra_simps powr_realpow[symmetric])
lp15@61609
  1557
    also have "\<dots> = \<lfloor>(2 * ai - 1) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>"
wenzelm@60500
  1558
      using \<open>b < 0\<close> assms
lp15@61609
  1559
      by (simp add: floor_divide_of_int_eq floor_eq floor_divide_real_eq_div
lp15@61609
  1560
        del: of_int_mult of_int_power of_int_diff)
immler@58985
  1561
    also have "\<dots> = \<lfloor>(2 * ai - 1) * 2 powr (q - p - 1)\<rfloor>"
immler@58985
  1562
      using assms by (simp add: algebra_simps divide_powr_uminus powr_realpow[symmetric])
wenzelm@60698
  1563
    finally show ?thesis
wenzelm@60698
  1564
      using \<open>b < 0\<close> by simp
wenzelm@60698
  1565
  qed
immler@58985
  1566
qed
immler@58985
  1567
wenzelm@60698
  1568
lemma log2_abs_int_add_less_half_sgn_eq:
wenzelm@60698
  1569
  fixes ai :: int
wenzelm@60698
  1570
    and b :: real
wenzelm@61945
  1571
  assumes "\<bar>b\<bar> \<le> 1/2"
wenzelm@60698
  1572
    and "ai \<noteq> 0"
lp15@61609
  1573
  shows "\<lfloor>log 2 \<bar>real_of_int ai + b\<bar>\<rfloor> = \<lfloor>log 2 \<bar>ai + sgn b / 2\<bar>\<rfloor>"
wenzelm@60698
  1574
proof (cases "b = 0")
wenzelm@60698
  1575
  case True
wenzelm@60698
  1576
  then show ?thesis by simp
immler@58985
  1577
next
wenzelm@60698
  1578
  case False
wenzelm@63040
  1579
  define k where "k = \<lfloor>log 2 \<bar>ai\<bar>\<rfloor>"
wenzelm@60698
  1580
  then have "\<lfloor>log 2 \<bar>ai\<bar>\<rfloor> = k"
wenzelm@60698
  1581
    by simp
wenzelm@60698
  1582
  then have k: "2 powr k \<le> \<bar>ai\<bar>" "\<bar>ai\<bar> < 2 powr (k + 1)"
wenzelm@60500
  1583
    by (simp_all add: floor_log_eq_powr_iff \<open>ai \<noteq> 0\<close>)
immler@58985
  1584
  have "k \<ge> 0"
immler@58985
  1585
    using assms by (auto simp: k_def)
wenzelm@63040
  1586
  define r where "r = \<bar>ai\<bar> - 2 ^ nat k"
immler@58985
  1587
  have r: "0 \<le> r" "r < 2 powr k"
wenzelm@60500
  1588
    using \<open>k \<ge> 0\<close> k
immler@58985
  1589
    by (auto simp: r_def k_def algebra_simps powr_add abs_if powr_int)
wenzelm@60698
  1590
  then have "r \<le> (2::int) ^ nat k - 1"
wenzelm@60500
  1591
    using \<open>k \<ge> 0\<close> by (auto simp: powr_int)
lp15@61609
  1592
  from this[simplified of_int_le_iff[symmetric]] \<open>0 \<le> k\<close>
immler@58985
  1593
  have r_le: "r \<le> 2 powr k - 1"
wenzelm@63356
  1594
    by (auto simp: algebra_simps powr_int)
immler@66912
  1595
      (metis of_int_1 of_int_add of_int_le_numeral_power_cancel_iff)
immler@58985
  1596
immler@58985
  1597
  have "\<bar>ai\<bar> = 2 powr k + r"
wenzelm@60500
  1598
    using \<open>k \<ge> 0\<close> by (auto simp: k_def r_def powr_realpow[symmetric])
immler@58985
  1599
wenzelm@61945
  1600
  have pos: "\<bar>b\<bar> < 1 \<Longrightarrow> 0 < 2 powr k + (r + b)" for b :: real
wenzelm@60500
  1601
    using \<open>0 \<le> k\<close> \<open>ai \<noteq> 0\<close>
immler@58985
  1602
    by (auto simp add: r_def powr_realpow[symmetric] abs_if sgn_if algebra_simps
nipkow@62390
  1603
      split: if_split_asm)
immler@58985
  1604
  have less: "\<bar>sgn ai * b\<bar> < 1"
immler@58985
  1605
    and less': "\<bar>sgn (sgn ai * b) / 2\<bar> < 1"
nipkow@62390
  1606
    using \<open>\<bar>b\<bar> \<le> _\<close> by (auto simp: abs_if sgn_if split: if_split_asm)
immler@58985
  1607
wenzelm@61945
  1608
  have floor_eq: "\<And>b::real. \<bar>b\<bar> \<le> 1 / 2 \<Longrightarrow>
immler@58985
  1609
      \<lfloor>log 2 (1 + (r + b) / 2 powr k)\<rfloor> = (if r = 0 \<and> b < 0 then -1 else 0)"
wenzelm@60500
  1610
    using \<open>k \<ge> 0\<close> r r_le
immler@58985
  1611
    by (auto simp: floor_log_eq_powr_iff powr_minus_divide field_simps sgn_if)
immler@58985
  1612
lp15@61609
  1613
  from \<open>real_of_int \<bar>ai\<bar> = _\<close> have "\<bar>ai + b\<bar> = 2 powr k + (r + sgn ai * b)"
wenzelm@63356
  1614
    using \<open>\<bar>b\<bar> \<le> _\<close> \<open>0 \<le> k\<close> r
immler@58985
  1615
    by (auto simp add: sgn_if abs_if)
immler@58985
  1616
  also have "\<lfloor>log 2 \<dots>\<rfloor> = \<lfloor>log 2 (2 powr k + r + sgn (sgn ai * b) / 2)\<rfloor>"
immler@58985
  1617
  proof -
immler@58985
  1618
    have "2 powr k + (r + (sgn ai) * b) = 2 powr k * (1 + (r + sgn ai * b) / 2 powr k)"
immler@58985
  1619
      by (simp add: field_simps)
immler@58985
  1620
    also have "\<lfloor>log 2 \<dots>\<rfloor> = k + \<lfloor>log 2 (1 + (r + sgn ai * b) / 2 powr k)\<rfloor>"
immler@58985
  1621
      using pos[OF less]
immler@58985
  1622
      by (subst log_mult) (simp_all add: log_mult powr_mult field_simps)
immler@58985
  1623
    also
immler@58985
  1624
    let ?if = "if r = 0 \<and> sgn ai * b < 0 then -1 else 0"
immler@58985
  1625
    have "\<lfloor>log 2 (1 + (r + sgn ai * b) / 2 powr k)\<rfloor> = ?if"
wenzelm@63356
  1626
      using \<open>\<bar>b\<bar> \<le> _\<close>
immler@58985
  1627
      by (intro floor_eq) (auto simp: abs_mult sgn_if)
immler@58985
  1628
    also
immler@58985
  1629
    have "\<dots> = \<lfloor>log 2 (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k)\<rfloor>"
immler@58985
  1630
      by (subst floor_eq) (auto simp: sgn_if)
immler@58985
  1631
    also have "k + \<dots> = \<lfloor>log 2 (2 powr k * (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k))\<rfloor>"
nipkow@63599
  1632
      unfolding int_add_floor
wenzelm@61945
  1633
      using pos[OF less'] \<open>\<bar>b\<bar> \<le> _\<close>
nipkow@63599
  1634
      by (simp add: field_simps add_log_eq_powr del: floor_add2)
immler@58985
  1635
    also have "2 powr k * (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k) =
immler@58985
  1636
        2 powr k + r + sgn (sgn ai * b) / 2"
immler@58985
  1637
      by (simp add: sgn_if field_simps)
immler@58985
  1638
    finally show ?thesis .
immler@58985
  1639
  qed
immler@58985
  1640
  also have "2 powr k + r + sgn (sgn ai * b) / 2 = \<bar>ai + sgn b / 2\<bar>"
lp15@61609
  1641
    unfolding \<open>real_of_int \<bar>ai\<bar> = _\<close>[symmetric] using \<open>ai \<noteq> 0\<close>
immler@58985
  1642
    by (auto simp: abs_if sgn_if algebra_simps)
immler@58985
  1643
  finally show ?thesis .
immler@58985
  1644
qed
immler@58985
  1645
wenzelm@60698
  1646
context
wenzelm@60698
  1647
begin
wenzelm@60698
  1648
wenzelm@60698
  1649
qualified lemma compute_far_float_plus_down:
wenzelm@60698
  1650
  fixes m1 e1 m2 e2 :: int
wenzelm@60698
  1651
    and p :: nat
immler@62420
  1652
  defines "k1 \<equiv> Suc p - nat (bitlen \<bar>m1\<bar>)"
immler@58985
  1653
  assumes H: "bitlen \<bar>m2\<bar> \<le> e1 - e2 - k1 - 2" "m1 \<noteq> 0" "m2 \<noteq> 0" "e1 \<ge> e2"
immler@58985
  1654
  shows "float_plus_down p (Float m1 e1) (Float m2 e2) =
immler@58985
  1655
    float_round_down p (Float (m1 * 2 ^ (Suc (Suc k1)) + sgn m2) (e1 - int k1 - 2))"
immler@58985
  1656
proof -
lp15@61609
  1657
  let ?a = "real_of_float (Float m1 e1)"
lp15@61609
  1658
  let ?b = "real_of_float (Float m2 e2)"
immler@58985
  1659
  let ?sum = "?a + ?b"
lp15@61609
  1660
  let ?shift = "real_of_int e2 - real_of_int e1 + real k1 + 1"
immler@58985
  1661
  let ?m1 = "m1 * 2 ^ Suc k1"
immler@58985
  1662
  let ?m2 = "m2 * 2 powr ?shift"
immler@58985
  1663
  let ?m2' = "sgn m2 / 2"
immler@58985
  1664
  let ?e = "e1 - int k1 - 1"
immler@58985
  1665
immler@58985
  1666
  have sum_eq: "?sum = (?m1 + ?m2) * 2 powr ?e"
immler@58985
  1667
    by (auto simp: powr_add[symmetric] powr_mult[symmetric] algebra_simps
immler@58985
  1668
      powr_realpow[symmetric] powr_mult_base)
immler@58985
  1669
immler@58985
  1670
  have "\<bar>?m2\<bar> * 2 < 2 powr (bitlen \<bar>m2\<bar> + ?shift + 1)"
lp15@65583
  1671
    by (auto simp: field_simps powr_add powr_mult_base powr_diff abs_mult)
immler@58985
  1672
  also have "\<dots> \<le> 2 powr 0"
immler@58985
  1673
    using H by (intro powr_mono) auto
immler@58985
  1674
  finally have abs_m2_less_half: "\<bar>?m2\<bar> < 1 / 2"
immler@58985
  1675
    by simp
immler@58985
  1676
lp15@61609
  1677
  then have "\<bar>real_of_int m2\<bar> < 2 powr -(?shift + 1)"
immler@63248
  1678
    unfolding powr_minus_divide by (auto simp: bitlen_alt_def field_simps powr_mult_base abs_mult)
lp15@61609
  1679
  also have "\<dots> \<le> 2 powr real_of_int (e1 - e2 - 2)"
immler@58985
  1680
    by simp
lp15@61609
  1681
  finally have b_less_quarter: "\<bar>?b\<bar> < 1/4 * 2 powr real_of_int e1"
lp15@65583
  1682
    by (simp add: powr_add field_simps powr_diff abs_mult)
lp15@61609
  1683
  also have "1/4 < \<bar>real_of_int m1\<bar> / 2" using \<open>m1 \<noteq> 0\<close> by simp
immler@58985
  1684
  finally have b_less_half_a: "\<bar>?b\<bar> < 1/2 * \<bar>?a\<bar>"
immler@58985
  1685
    by (simp add: algebra_simps powr_mult_base abs_mult)
wenzelm@60698
  1686
  then have a_half_less_sum: "\<bar>?a\<bar> / 2 < \<bar>?sum\<bar>"
nipkow@62390
  1687
    by (auto simp: field_simps abs_if split: if_split_asm)
immler@58985
  1688
immler@58985
  1689
  from b_less_half_a have "\<bar>?b\<bar> < \<bar>?a\<bar>" "\<bar>?b\<bar> \<le> \<bar>?a\<bar>"
immler@58985
  1690
    by simp_all
immler@58985
  1691
lp15@61609
  1692
  have "\<bar>real_of_float (Float m1 e1)\<bar> \<ge> 1/4 * 2 powr real_of_int e1"
wenzelm@60500
  1693
    using \<open>m1 \<noteq> 0\<close>
immler@58985
  1694
    by (auto simp: powr_add powr_int bitlen_nonneg divide_right_mono abs_mult)
wenzelm@60698
  1695
  then have "?sum \<noteq> 0" using b_less_quarter
immler@58985
  1696
    by (rule sum_neq_zeroI)
wenzelm@60698
  1697
  then have "?m1 + ?m2 \<noteq> 0"
immler@58985
  1698
    unfolding sum_eq by (simp add: abs_mult zero_less_mult_iff)
immler@58985
  1699
lp15@61609
  1700
  have "\<bar>real_of_int ?m1\<bar> \<ge> 2 ^ Suc k1" "\<bar>?m2'\<bar> < 2 ^ Suc k1"
wenzelm@60500
  1701
    using \<open>m1 \<noteq> 0\<close> \<open>m2 \<noteq> 0\<close> by (auto simp: sgn_if less_1_mult abs_mult simp del: power.simps)
wenzelm@60698
  1702
  then have sum'_nz: "?m1 + ?m2' \<noteq> 0"
immler@58985
  1703
    by (intro sum_neq_zeroI)
immler@58985
  1704
lp15@61609
  1705
  have "\<lfloor>log 2 \<bar>real_of_float (Float m1 e1) + real_of_float (Float m2 e2)\<bar>\<rfloor> = \<lfloor>log 2 \<bar>?m1 + ?m2\<bar>\<rfloor> + ?e"
wenzelm@60500
  1706
    using \<open>?m1 + ?m2 \<noteq> 0\<close>
immler@58985
  1707
    unfolding floor_add[symmetric] sum_eq
lp15@61609
  1708
    by (simp add: abs_mult log_mult) linarith
lp15@61609
  1709
  also have "\<lfloor>log 2 \<bar>?m1 + ?m2\<bar>\<rfloor> = \<lfloor>log 2 \<bar>?m1 + sgn (real_of_int m2 * 2 powr ?shift) / 2\<bar>\<rfloor>"
wenzelm@60500
  1710
    using abs_m2_less_half \<open>m1 \<noteq> 0\<close>
immler@58985
  1711
    by (intro log2_abs_int_add_less_half_sgn_eq) (auto simp: abs_mult)
lp15@61609
  1712
  also have "sgn (real_of_int m2 * 2 powr ?shift) = sgn m2"
immler@58985
  1713
    by (auto simp: sgn_if zero_less_mult_iff less_not_sym)
immler@58985
  1714
  also
immler@58985
  1715
  have "\<bar>?m1 + ?m2'\<bar> * 2 powr ?e = \<bar>?m1 * 2 + sgn m2\<bar> * 2 powr (?e - 1)"
lp15@65583
  1716
    by (auto simp: field_simps powr_minus[symmetric] powr_diff powr_mult_base)
lp15@61609
  1717
  then have "\<lfloor>log 2 \<bar>?m1 + ?m2'\<bar>\<rfloor> + ?e = \<lfloor>log 2 \<bar>real_of_float (Float (?m1 * 2 + sgn m2) (?e - 1))\<bar>\<rfloor>"
wenzelm@60500
  1718
    using \<open>?m1 + ?m2' \<noteq> 0\<close>
nipkow@63599
  1719
    unfolding floor_add_int
nipkow@63599
  1720
    by (simp add: log_add_eq_powr abs_mult_pos del: floor_add2)
immler@58985
  1721
  finally
lp15@61609
  1722
  have "\<lfloor>log 2 \<bar>?sum\<bar>\<rfloor> = \<lfloor>log 2 \<bar>real_of_float (Float (?m1*2 + sgn m2) (?e - 1))\<bar>\<rfloor>" .
wenzelm@60698
  1723
  then have "plus_down p (Float m1 e1) (Float m2 e2) =
immler@58985
  1724
      truncate_down p (Float (?m1*2 + sgn m2) (?e - 1))"
immler@58985
  1725
    unfolding plus_down_def
immler@58985
  1726
  proof (rule truncate_down_log2_eqI)
immler@62420
  1727
    let ?f = "(int p - \<lfloor>log 2 \<bar>real_of_float (Float m1 e1) + real_of_float (Float m2 e2)\<bar>\<rfloor>)"
immler@58985
  1728
    let ?ai = "m1 * 2 ^ (Suc k1)"
lp15@61609
  1729
    have "\<lfloor>(?a + ?b) * 2 powr real_of_int ?f\<rfloor> = \<lfloor>(real_of_int (2 * ?ai) + sgn ?b) * 2 powr real_of_int (?f - - ?e - 1)\<rfloor>"
immler@58985
  1730
    proof (rule floor_sum_times_2_powr_sgn_eq)
lp15@61609
  1731
      show "?a * 2 powr real_of_int (-?e) = real_of_int ?ai"
lp15@65583
  1732
        by (simp add: powr_add powr_realpow[symmetric] powr_diff)
lp15@61609
  1733
      show "\<bar>?b * 2 powr real_of_int (-?e + 1)\<bar> \<le> 1"
immler@58985
  1734
        using abs_m2_less_half
immler@58985
  1735
        by (simp add: abs_mult powr_add[symmetric] algebra_simps powr_mult_base)
immler@58985
  1736
    next
lp15@61609
  1737
      have "e1 + \<lfloor>log 2 \<bar>real_of_int m1\<bar>\<rfloor> - 1 = \<lfloor>log 2 \<bar>?a\<bar>\<rfloor> - 1"
wenzelm@60500
  1738
        using \<open>m1 \<noteq> 0\<close>
nipkow@63599
  1739
        by (simp add: int_add_floor algebra_simps log_mult abs_mult del: floor_add2)
immler@58985
  1740
      also have "\<dots> \<le> \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor>"
wenzelm@60500
  1741
        using a_half_less_sum \<open>m1 \<noteq> 0\<close> \<open>?sum \<noteq> 0\<close>
lp15@61609
  1742
        unfolding floor_diff_of_int[symmetric]
lp15@61609
  1743
        by (auto simp add: log_minus_eq_powr powr_minus_divide intro!: floor_mono)
immler@58985
  1744
      finally
immler@58985
  1745
      have "int p - \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor> \<le> p - (bitlen \<bar>m1\<bar>) - e1 + 2"
immler@63248
  1746
        by (auto simp: algebra_simps bitlen_alt_def \<open>m1 \<noteq> 0\<close>)
immler@62420
  1747
      also have "\<dots> \<le> - ?e"
immler@58985
  1748
        using bitlen_nonneg[of "\<bar>m1\<bar>"] by (simp add: k1_def)
immler@58985
  1749
      finally show "?f \<le> - ?e" by simp
immler@58985
  1750
    qed
immler@58985
  1751
    also have "sgn ?b = sgn m2"
immler@58985
  1752
      using powr_gt_zero[of 2 e2]
immler@58985
  1753
      by (auto simp add: sgn_if zero_less_mult_iff simp del: powr_gt_zero)
lp15@61609
  1754
    also have "\<lfloor>(real_of_int (2 * ?m1) + real_of_int (sgn m2)) * 2 powr real_of_int (?f - - ?e - 1)\<rfloor> =
immler@58985
  1755
        \<lfloor>Float (?m1 * 2 + sgn m2) (?e - 1) * 2 powr ?f\<rfloor>"
immler@58985
  1756
      by (simp add: powr_add[symmetric] algebra_simps powr_realpow[symmetric])
immler@58985
  1757
    finally
lp15@61609
  1758
    show "\<lfloor>(?a + ?b) * 2 powr ?f\<rfloor> = \<lfloor>real_of_float (Float (?m1 * 2 + sgn m2) (?e - 1)) * 2 powr ?f\<rfloor>" .
immler@58985
  1759
  qed
wenzelm@60698
  1760
  then show ?thesis
immler@58985
  1761
    by transfer (simp add: plus_down_def ac_simps Let_def)
immler@58985
  1762
qed
immler@58985
  1763
immler@58985
  1764
lemma compute_float_plus_down_naive[code]: "float_plus_down p x y = float_round_down p (x + y)"
immler@58985
  1765
  by transfer (auto simp: plus_down_def)
immler@58985
  1766
wenzelm@60698
  1767
qualified lemma compute_float_plus_down[code]:
immler@58985
  1768
  fixes p::nat and m1 e1 m2 e2::int
immler@58985
  1769
  shows "float_plus_down p (Float m1 e1) (Float m2 e2) =
immler@58985
  1770
    (if m1 = 0 then float_round_down p (Float m2 e2)
immler@58985
  1771
    else if m2 = 0 then float_round_down p (Float m1 e1)
wenzelm@63356
  1772
    else
wenzelm@63356
  1773
      (if e1 \<ge> e2 then
wenzelm@63356
  1774
        (let k1 = Suc p - nat (bitlen \<bar>m1\<bar>) in
wenzelm@63356
  1775
          if bitlen \<bar>m2\<bar> > e1 - e2 - k1 - 2
wenzelm@63356
  1776
          then float_round_down p ((Float m1 e1) + (Float m2 e2))
wenzelm@63356
  1777
          else float_round_down p (Float (m1 * 2 ^ (Suc (Suc k1)) + sgn m2) (e1 - int k1 - 2)))
immler@58985
  1778
    else float_plus_down p (Float m2 e2) (Float m1 e1)))"
immler@58985
  1779
proof -
immler@58985
  1780
  {
immler@62420
  1781
    assume "bitlen \<bar>m2\<bar> \<le> e1 - e2 - (Suc p - nat (bitlen \<bar>m1\<bar>)) - 2" "m1 \<noteq> 0" "m2 \<noteq> 0" "e1 \<ge> e2"
wenzelm@60698
  1782
    note compute_far_float_plus_down[OF this]
immler@58985
  1783
  }
wenzelm@60698
  1784
  then show ?thesis
immler@58985
  1785
    by transfer (simp add: Let_def plus_down_def ac_simps)
immler@58985
  1786
qed
immler@58985
  1787
wenzelm@60698
  1788
qualified lemma compute_float_plus_up[code]: "float_plus_up p x y = - float_plus_down p (-x) (-y)"
immler@58985
  1789
  using truncate_down_uminus_eq[of p "x + y"]
immler@58985
  1790
  by transfer (simp add: plus_down_def plus_up_def ac_simps)
immler@58985
  1791
immler@58985
  1792
lemma mantissa_zero[simp]: "mantissa 0 = 0"
wenzelm@60698
  1793
  by (metis mantissa_0 zero_float.abs_eq)
wenzelm@60698
  1794
immler@62421
  1795
qualified lemma compute_float_less[code]: "a < b \<longleftrightarrow> is_float_pos (float_plus_down 0 b (- a))"
immler@62421
  1796
  using truncate_down[of 0 "b - a"] truncate_down_pos[of "b - a" 0]
immler@62421
  1797
  by transfer (auto simp: plus_down_def)
immler@62421
  1798
immler@62421
  1799
qualified lemma compute_float_le[code]: "a \<le> b \<longleftrightarrow> is_float_nonneg (float_plus_down 0 b (- a))"
immler@62421
  1800
  using truncate_down[of 0 "b - a"] truncate_down_nonneg[of "b - a" 0]
immler@62421
  1801
  by transfer (auto simp: plus_down_def)
immler@62421
  1802
wenzelm@60698
  1803
end
immler@58985
  1804
immler@58985
  1805
wenzelm@60500
  1806
subsection \<open>Lemmas needed by Approximate\<close>
hoelzl@47599
  1807
wenzelm@60698
  1808
lemma Float_num[simp]:
lp15@61609
  1809
   "real_of_float (Float 1 0) = 1"
lp15@61609
  1810
   "real_of_float (Float 1 1) = 2"
lp15@61609
  1811
   "real_of_float (Float 1 2) = 4"
lp15@61609
  1812
   "real_of_float (Float 1 (- 1)) = 1/2"
lp15@61609
  1813
   "real_of_float (Float 1 (- 2)) = 1/4"
lp15@61609
  1814
   "real_of_float (Float 1 (- 3)) = 1/8"
lp15@61609
  1815
   "real_of_float (Float (- 1) 0) = -1"
immler@62420
  1816
   "real_of_float (Float (numeral n) 0) = numeral n"
immler@62420
  1817
   "real_of_float (Float (- numeral n) 0) = - numeral n"
wenzelm@60698
  1818
  using two_powr_int_float[of 2] two_powr_int_float[of "-1"] two_powr_int_float[of "-2"]
wenzelm@60698
  1819
    two_powr_int_float[of "-3"]
wenzelm@60698
  1820
  using powr_realpow[of 2 2] powr_realpow[of 2 3]
lp15@65583
  1821
  using powr_minus[of "2::real" 1] powr_minus[of "2::real" 2] powr_minus[of "2::real" 3]
wenzelm@60698
  1822
  by auto
hoelzl@47599
  1823
lp15@61609
  1824
lemma real_of_Float_int[simp]: "real_of_float (Float n 0) = real n"
wenzelm@60698
  1825
  by simp
hoelzl@47599
  1826
lp15@61609
  1827
lemma float_zero[simp]: "real_of_float (Float 0 e) = 0"
wenzelm@60698
  1828
  by simp
hoelzl@47599
  1829
wenzelm@61945
  1830
lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> \<bar>(a::int) div 2\<bar> < \<bar>a\<bar>"
wenzelm@60698
  1831
  by arith
hoelzl@29804
  1832
lp15@61609
  1833
lemma lapprox_rat: "real_of_float (lapprox_rat prec x y) \<le> real_of_int x / real_of_int y"
immler@62420
  1834
  by (simp add: lapprox_rat.rep_eq truncate_down)
obua@16782
  1835
wenzelm@60698
  1836
lemma mult_div_le:
wenzelm@60698
  1837
  fixes a b :: int
wenzelm@60698
  1838
  assumes "b > 0"
wenzelm@60698
  1839
  shows "a \<ge> b * (a div b)"
hoelzl@47599
  1840
proof -
haftmann@64246
  1841
  from minus_div_mult_eq_mod [symmetric, of a b] have "a = b * (a div b) + a mod b"
wenzelm@60698
  1842
    by simp
wenzelm@60698
  1843
  also have "\<dots> \<ge> b * (a div b) + 0"
wenzelm@60698
  1844
    apply (rule add_left_mono)
wenzelm@60698
  1845
    apply (rule pos_mod_sign)
wenzelm@63356
  1846
    using assms
wenzelm@63356
  1847
    apply simp
wenzelm@60698
  1848
    done
wenzelm@60698
  1849
  finally show ?thesis
wenzelm@60698
  1850
    by simp
hoelzl@47599
  1851
qed
hoelzl@47599
  1852
hoelzl@47599
  1853
lemma lapprox_rat_nonneg:
immler@58982
  1854
  assumes "0 \<le> x" and "0 \<le> y"
lp15@61609
  1855
  shows "0 \<le> real_of_float (lapprox_rat n x y)"
immler@62420
  1856
  using assms
immler@62420
  1857
  by transfer (simp add: truncate_down_nonneg)
obua@16782
  1858
lp15@61609
  1859
lemma rapprox_rat: "real_of_int x / real_of_int y \<le> real_of_float (rapprox_rat prec x y)"
immler@62420
  1860
  by transfer (simp add: truncate_up)
hoelzl@47599
  1861
hoelzl@47599
  1862
lemma rapprox_rat_le1:
wenzelm@63356
  1863
  assumes "0 \<le> x" "0 < y" "x \<le> y"
lp15@61609
  1864
  shows "real_of_float (rapprox_rat n x y) \<le> 1"
immler@62420
  1865
  using assms
immler@62420
  1866
  by transfer (simp add: truncate_up_le1)
obua@16782
  1867
lp15@61609
  1868
lemma rapprox_rat_nonneg_nonpos: "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> real_of_float (rapprox_rat n x y) \<le> 0"
immler@62420
  1869
  by transfer (simp add: truncate_up_nonpos divide_nonneg_nonpos)
obua@16782
  1870
lp15@61609
  1871
lemma rapprox_rat_nonpos_nonneg: "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> real_of_float (rapprox_rat n x y) \<le> 0"
immler@62420
  1872
  by transfer (simp add: truncate_up_nonpos divide_nonpos_nonneg)
obua@16782
  1873
immler@54782
  1874
lemma real_divl: "real_divl prec x y \<le> x / y"
immler@62420
  1875
  by (simp add: real_divl_def truncate_down)
immler@54782
  1876
immler@54782
  1877
lemma real_divr: "x / y \<le> real_divr prec x y"
immler@62420
  1878
  by (simp add: real_divr_def truncate_up)
immler@54782
  1879
lp15@61609
  1880
lemma float_divl: "real_of_float (float_divl prec x y) \<le> x / y"
immler@54782
  1881
  by transfer (rule real_divl)
immler@54782
  1882
wenzelm@63356
  1883
lemma real_divl_lower_bound: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> real_divl prec x y"
immler@62420
  1884
  by (simp add: real_divl_def truncate_down_nonneg)
hoelzl@47599
  1885
wenzelm@63356
  1886
lemma float_divl_lower_bound: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> real_of_float (float_divl prec x y)"
immler@54782
  1887
  by transfer (rule real_divl_lower_bound)
hoelzl@47599
  1888
hoelzl@47599
  1889
lemma exponent_1: "exponent 1 = 0"
hoelzl@47599
  1890
  using exponent_float[of 1 0] by (simp add: one_float_def)
hoelzl@47599
  1891
hoelzl@47599
  1892
lemma mantissa_1: "mantissa 1 = 1"
hoelzl@47599
  1893
  using mantissa_float[of 1 0] by (simp add: one_float_def)
obua@16782
  1894
hoelzl@47599
  1895
lemma bitlen_1: "bitlen 1 = 1"
immler@63248
  1896
  by (simp add: bitlen_alt_def)
hoelzl@47599
  1897
hoelzl@47599
  1898
lemma float_upper_bound: "x \<le> 2 powr (bitlen \<bar>mantissa x\<bar> + exponent x)"
wenzelm@60698
  1899
proof (cases "x = 0")
wenzelm@60698
  1900
  case True
wenzelm@60698
  1901
  then show ?thesis by simp
wenzelm@60698
  1902
next
wenzelm@60698
  1903
  case False
wenzelm@60698
  1904
  then have "mantissa x \<noteq> 0"
wenzelm@60698
  1905
    using mantissa_eq_zero_iff by auto
wenzelm@60698
  1906
  have "x = mantissa x * 2 powr (exponent x)"
wenzelm@60698
  1907
    by (rule mantissa_exponent)
wenzelm@60698
  1908
  also have "mantissa x \<le> \<bar>mantissa x\<bar>"
wenzelm@60698
  1909
    by simp
wenzelm@60698
  1910
  also have "\<dots> \<le> 2 powr (bitlen \<bar>mantissa x\<bar>)"
wenzelm@60500
  1911
    using bitlen_bounds[of "\<bar>mantissa x\<bar>"] bitlen_nonneg \<open>mantissa x \<noteq> 0\<close>
lp15@61649
  1912
    by (auto simp del: of_int_abs simp add: powr_int)
hoelzl@47599
  1913
  finally show ?thesis by (simp add: powr_add)
hoelzl@29804
  1914
qed
hoelzl@29804
  1915
immler@54782
  1916
lemma real_divl_pos_less1_bound:
immler@62420
  1917
  assumes "0 < x" "x \<le> 1"
immler@58982
  1918
  shows "1 \<le> real_divl prec 1 x"
immler@62420
  1919
  using assms
immler@62420
  1920
  by (auto intro!: truncate_down_ge1 simp: real_divl_def)
obua@16782
  1921
immler@54782
  1922
lemma float_divl_pos_less1_bound:
wenzelm@63356
  1923
  "0 < real_of_float x \<Longrightarrow> real_of_float x \<le> 1 \<Longrightarrow> prec \<ge> 1 \<Longrightarrow>
wenzelm@63356
  1924
    1 \<le> real_of_float (float_divl prec 1 x)"
wenzelm@60698
  1925
  by transfer (rule real_divl_pos_less1_bound)
obua@16782
  1926
lp15@61609
  1927
lemma float_divr: "real_of_float x / real_of_float y \<le> real_of_float (float_divr prec x y)"
immler@54782
  1928
  by transfer (rule real_divr)
immler@54782
  1929
wenzelm@60698
  1930
lemma real_divr_pos_less1_lower_bound:
wenzelm@60698
  1931
  assumes "0 < x"
wenzelm@60698
  1932
    and "x \<le> 1"
wenzelm@60698
  1933
  shows "1 \<le> real_divr prec 1 x"
hoelzl@29804
  1934
proof -
wenzelm@60698
  1935
  have "1 \<le> 1 / x"
wenzelm@63356
  1936
    using \<open>0 < x\<close> and \<open>x \<le> 1\<close> by auto
wenzelm@60698
  1937
  also have "\<dots> \<le> real_divr prec 1 x"
wenzelm@63356
  1938
    using real_divr[where x = 1 and y = x] by auto
hoelzl@47600
  1939
  finally show ?thesis by auto
hoelzl@29804
  1940
qed
hoelzl@29804
  1941
immler@58982
  1942
lemma float_divr_pos_less1_lower_bound: "0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 1 \<le> float_divr prec 1 x"
immler@54782
  1943
  by transfer (rule real_divr_pos_less1_lower_bound)
immler@54782
  1944
wenzelm@63356
  1945
lemma real_divr_nonpos_pos_upper_bound: "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> real_divr prec x y \<le> 0"
immler@62420
  1946
  by (simp add: real_divr_def truncate_up_nonpos divide_le_0_iff)
immler@54782
  1947
hoelzl@47599
  1948
lemma float_divr_nonpos_pos_upper_bound:
lp15@61609
  1949
  "real_of_float x \<le> 0 \<Longrightarrow> 0 \<le> real_of_float y \<Longrightarrow> real_of_float (float_divr prec x y) \<le> 0"
immler@54782
  1950
  by transfer (rule real_divr_nonpos_pos_upper_bound)
immler@54782
  1951
wenzelm@63356
  1952
lemma real_divr_nonneg_neg_upper_bound: "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> real_divr prec x y \<le> 0"
immler@62420
  1953
  by (simp add: real_divr_def truncate_up_nonpos divide_le_0_iff)
obua@16782
  1954
hoelzl@47599
  1955
lemma float_divr_nonneg_neg_upper_bound:
lp15@61609
  1956
  "0 \<le> real_of_float x \<Longrightarrow> real_of_float y \<le> 0 \<Longrightarrow> real_of_float (float_divr prec x y) \<le> 0"
immler@54782
  1957
  by transfer (rule real_divr_nonneg_neg_upper_bound)
immler@54782
  1958
immler@54784
  1959
lemma truncate_up_nonneg_mono:
immler@54784
  1960
  assumes "0 \<le> x" "x \<le> y"
immler@54784
  1961
  shows "truncate_up prec x \<le> truncate_up prec y"
immler@54784
  1962
proof -
wenzelm@60698
  1963
  consider "\<lfloor>log 2 x\<rfloor> = \<lfloor>log 2 y\<rfloor>" | "\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>" "0 < x" | "x \<le> 0"
wenzelm@60698
  1964
    by arith
wenzelm@60698
  1965
  then show ?thesis
wenzelm@60698
  1966
  proof cases
wenzelm@60698
  1967
    case 1
wenzelm@60698
  1968
    then show ?thesis
immler@54784
  1969
      using assms
immler@54784
  1970
      by (auto simp: truncate_up_def round_up_def intro!: ceiling_mono)
wenzelm@60698
  1971
  next
wenzelm@60698
  1972
    case 2
wenzelm@60698
  1973
    from assms \<open>0 < x\<close> have "log 2 x \<le> log 2 y"
wenzelm@60698
  1974
      by auto
wenzelm@60698
  1975
    with \<open>\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>\<close>
wenzelm@60698
  1976
    have logless: "log 2 x < log 2 y" and flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>"
wenzelm@60698
  1977
      by (metis floor_less_cancel linorder_cases not_le)+
immler@54784
  1978
    have "truncate_up prec x =
immler@62420
  1979
      real_of_int \<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor> )\<rceil> * 2 powr - real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)"
immler@54784
  1980
      using assms by (simp add: truncate_up_def round_up_def)
immler@62420
  1981
    also have "\<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)\<rceil> \<le> (2 ^ (Suc prec))"
wenzelm@63356
  1982
    proof (simp only: ceiling_le_iff)
wenzelm@63356
  1983
      have "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le>
wenzelm@63356
  1984
        x * (2 powr real (Suc prec) / (2 powr log 2 x))"
immler@54784
  1985
        using real_of_int_floor_add_one_ge[of "log 2 x"] assms
lp15@65583
  1986
        by (auto simp add: algebra_simps powr_diff [symmetric]  intro!: mult_left_mono)
immler@62420
  1987
      then show "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> real_of_int ((2::int) ^ (Suc prec))"
immler@62420
  1988
        using \<open>0 < x\<close> by (simp add: powr_realpow powr_add)
immler@54784
  1989
    qed
immler@62420
  1990
    then have "real_of_int \<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)\<rceil> \<le> 2 powr int (Suc prec)"
wenzelm@63356
  1991
      by (auto simp: powr_realpow powr_add)
immler@66912
  1992
        (metis power_Suc of_int_le_numeral_power_cancel_iff)
immler@54784
  1993
    also
immler@62420
  1994
    have "2 powr - real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> 2 powr - real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)"
immler@54784
  1995
      using logless flogless by (auto intro!: floor_mono)
wenzelm@63356
  1996
    also have "2 powr real_of_int (int (Suc prec)) \<le>
wenzelm@63356
  1997
        2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1))"
wenzelm@60500
  1998
      using assms \<open>0 < x\<close>
immler@54784
  1999
      by (auto simp: algebra_simps)
wenzelm@63356
  2000
    finally have "truncate_up prec x \<le>
wenzelm@63356
  2001
        2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)) * 2 powr - real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)"
immler@54784
  2002
      by simp
lp15@61609
  2003
    also have "\<dots> = 2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor>) - real_of_int (int prec - \<lfloor>log 2 y\<rfloor>))"
immler@54784
  2004
      by (subst powr_add[symmetric]) simp
immler@54784
  2005
    also have "\<dots> = y"
wenzelm@60500
  2006
      using \<open>0 < x\<close> assms
immler@54784
  2007
      by (simp add: powr_add)
immler@54784
  2008
    also have "\<dots> \<le> truncate_up prec y"
immler@54784
  2009
      by (rule truncate_up)
wenzelm@60698
  2010
    finally show ?thesis .
wenzelm@60698
  2011
  next
wenzelm@60698
  2012
    case 3
wenzelm@60698
  2013
    then show ?thesis
immler@54784
  2014
      using assms
immler@54784
  2015
      by (auto intro!: truncate_up_le)
wenzelm@60698
  2016
  qed
immler@54784
  2017
qed
immler@54784
  2018
immler@54784
  2019
lemma truncate_up_switch_sign_mono:
immler@54784
  2020
  assumes "x \<le> 0" "0 \<le> y"
immler@54784
  2021
  shows "truncate_up prec x \<le> truncate_up prec y"
immler@54784
  2022
proof -
wenzelm@60500
  2023
  note truncate_up_nonpos[OF \<open>x \<le> 0\<close>]
wenzelm@60500
  2024
  also note truncate_up_le[OF \<open>0 \<le> y\<close>]
immler@54784
  2025
  finally show ?thesis .
immler@54784
  2026
qed
immler@54784
  2027
immler@54784
  2028
lemma truncate_down_switch_sign_mono:
wenzelm@60698
  2029
  assumes "x \<le> 0"
wenzelm@60698
  2030
    and "0 \<le> y"
wenzelm@60698
  2031
    and "x \<le> y"
immler@54784
  2032
  shows "truncate_down prec x \<le> truncate_down prec y"
immler@54784
  2033
proof -
wenzelm@60500
  2034
  note truncate_down_le[OF \<open>x \<le> 0\<close>]
wenzelm@60500
  2035
  also note truncate_down_nonneg[OF \<open>0 \<le> y\<close>]
immler@54784
  2036
  finally show ?thesis .
immler@54784
  2037
qed
immler@54784
  2038
immler@54784
  2039
lemma truncate_down_nonneg_mono:
immler@54784
  2040
  assumes "0 \<le> x" "x \<le> y"
immler@54784
  2041
  shows "truncate_down prec x \<le> truncate_down prec y"
immler@54784
  2042
proof -
immler@62420
  2043
  consider "x \<le> 0" | "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> = \<lfloor>log 2 \<bar>y\<bar>\<rfloor>" |
immler@62420
  2044
    "0 < x" "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>"
wenzelm@60698
  2045
    by arith
wenzelm@60698
  2046
  then show ?thesis
wenzelm@60698
  2047
  proof cases
wenzelm@60698
  2048
    case 1
immler@54784
  2049
    with assms have "x = 0" "0 \<le> y" by simp_all
wenzelm@60698
  2050
    then show ?thesis
immler@58985
  2051
      by (auto intro!: truncate_down_nonneg)
wenzelm@60698
  2052
  next
immler@62420
  2053
    case 2
wenzelm@60698
  2054
    then show ?thesis
immler@54784
  2055
      using assms
immler@54784
  2056
      by (auto simp: truncate_down_def round_down_def intro!: floor_mono)
wenzelm@60698
  2057
  next
immler@62420
  2058
    case 3
wenzelm@60698
  2059
    from \<open>0 < x\<close> have "log 2 x \<le> log 2 y" "0 < y" "0 \<le> y"
wenzelm@60698
  2060
      using assms by auto
wenzelm@60698
  2061
    with \<open>\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>\<close>
wenzelm@60698
  2062
    have logless: "log 2 x < log 2 y" and flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>"
wenzelm@60500
  2063
      unfolding atomize_conj abs_of_pos[OF \<open>0 < x\<close>] abs_of_pos[OF \<open>0 < y\<close>]
immler@54784
  2064
      by (metis floor_less_cancel linorder_cases not_le)
immler@62420
  2065
    have "2 powr prec \<le> y * 2 powr real prec / (2 powr log 2 y)"
wenzelm@60698
  2066
      using \<open>0 < y\<close> by simp
immler@62420
  2067
    also have "\<dots> \<le> y * 2 powr real (Suc prec) / (2 powr (real_of_int \<lfloor>log 2 y\<rfloor> + 1))"
wenzelm@60500
  2068
      using \<open>0 \<le> y\<close> \<open>0 \<le> x\<close> assms(2)
nipkow@56544
  2069
      by (auto intro!: powr_mono divide_left_mono
lp15@65583
  2070
          simp: of_nat_diff powr_add powr_diff)
immler@62420
  2071
    also have "\<dots> = y * 2 powr real (Suc prec) / (2 powr real_of_int \<lfloor>log 2 y\<rfloor> * 2)"
immler@54784
  2072
      by (auto simp: powr_add)
immler@62420
  2073
    finally have "(2 ^ prec) \<le> \<lfloor>y * 2 powr real_of_int (int (Suc prec) - \<lfloor>log 2 \<bar>y\<bar>\<rfloor> - 1)\<rfloor>"
wenzelm@60500
  2074
      using \<open>0 \<le> y\<close>
lp15@65583
  2075
      by (auto simp: powr_diff le_floor_iff powr_realpow powr_add)
immler@62420
  2076
    then have "(2 ^ (prec)) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>) \<le> truncate_down prec y"
immler@54784
  2077
      by (auto simp: truncate_down_def round_down_def)
wenzelm@63356
  2078
    moreover have "x \<le> (2 ^ prec) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>)"
wenzelm@63356
  2079
    proof -
wenzelm@60500
  2080
      have "x = 2 powr (log 2 \<bar>x\<bar>)" using \<open>0 < x\<close> by simp
immler@62420
  2081
      also have "\<dots> \<le> (2 ^ (Suc prec )) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)"
immler@62420
  2082
        using real_of_int_floor_add_one_ge[of "log 2 \<bar>x\<bar>"] \<open>0 < x\<close>
immler@62420
  2083
        by (auto simp: powr_realpow[symmetric] powr_add[symmetric] algebra_simps
immler@62420
  2084
          powr_mult_base le_powr_iff)
immler@54784
  2085
      also
immler@62420
  2086
      have "2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) \<le> 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor> + 1)"
wenzelm@60500
  2087
        using logless flogless \<open>x > 0\<close> \<open>y > 0\<close>
immler@54784
  2088
        by (auto intro!: floor_mono)
wenzelm@63356
  2089
      finally show ?thesis
lp15@65583
  2090
        by (auto simp: powr_realpow[symmetric] powr_diff assms of_nat_diff)
wenzelm@63356
  2091
    qed
wenzelm@60698
  2092
    ultimately show ?thesis
immler@54784
  2093
      by (metis dual_order.trans truncate_down)
wenzelm@60698
  2094
  qed
immler@54784
  2095
qed
immler@54784
  2096
immler@58982
  2097
lemma truncate_down_eq_truncate_up: "truncate_down p x = - truncate_up p (-x)"
immler@58982
  2098
  and truncate_up_eq_truncate_down: "truncate_up p x = - truncate_down p (-x)"
immler@58982
  2099
  by (auto simp: truncate_up_uminus_eq truncate_down_uminus_eq)
immler@58982
  2100
immler@54784
  2101
lemma truncate_down_mono: "x \<le> y \<Longrightarrow> truncate_down p x \<le> truncate_down p y"
immler@54784
  2102
  apply (cases "0 \<le> x")
immler@54784
  2103
  apply (rule truncate_down_nonneg_mono, assumption+)
immler@58982
  2104
  apply (simp add: truncate_down_eq_truncate_up)
immler@54784
  2105
  apply (cases "0 \<le> y")
immler@54784
  2106
  apply (auto intro: truncate_up_nonneg_mono truncate_up_switch_sign_mono)
immler@54784
  2107
  done
immler@54784
  2108
immler@54784
  2109
lemma truncate_up_mono: "x \<le> y \<Longrightarrow> truncate_up p x \<le> truncate_up p y"
immler@58982
  2110
  by (simp add: truncate_up_eq_truncate_down truncate_down_mono)
immler@54784
  2111
hoelzl@47599
  2112
lemma Float_le_zero_iff: "Float a b \<le> 0 \<longleftrightarrow> a \<le> 0"
immler@67573
  2113
  by (auto simp: zero_float_def mult_le_0_iff)
hoelzl@47599
  2114
wenzelm@60698
  2115
lemma real_of_float_pprt[simp]:
wenzelm@60698
  2116
  fixes a :: float
lp15@61609
  2117
  shows "real_of_float (pprt a) = pprt (real_of_float a)"
hoelzl@47600
  2118
  unfolding pprt_def sup_float_def max_def sup_real_def by auto
hoelzl@47599
  2119
wenzelm@60698
  2120
lemma real_of_float_nprt[simp]:
wenzelm@60698
  2121
  fixes a :: float
lp15@61609
  2122
  shows "real_of_float (nprt a) = nprt (real_of_float a)"
hoelzl@47600
  2123
  unfolding nprt_def inf_float_def min_def inf_real_def by auto
hoelzl@47599
  2124
wenzelm@60698
  2125
context
wenzelm@60698
  2126
begin
wenzelm@60698
  2127
kuncar@55565
  2128
lift_definition int_floor_fl :: "float \<Rightarrow> int" is floor .
obua@16782
  2129
wenzelm@60698
  2130
qualified lemma compute_int_floor_fl[code]:
hoelzl@47601
  2131
  "int_floor_fl (Float m e) = (if 0 \<le> e then m * 2 ^ nat e else m div (2 ^ (nat (-e))))"
lp15@61609
  2132
  apply transfer
lp15@61609
  2133
  apply (simp add: powr_int floor_divide_of_int_eq)
wenzelm@61942
  2134
  apply (metis (no_types, hide_lams)floor_divide_of_int_eq of_int_numeral of_int_power floor_of_int of_int_mult)
wenzelm@61942
  2135
  done
hoelzl@47599
  2136
wenzelm@61942
  2137
lift_definition floor_fl :: "float \<Rightarrow> float" is "\<lambda>x. real_of_int \<lfloor>x\<rfloor>"
wenzelm@61942
  2138
  by simp
hoelzl@47599
  2139
wenzelm@60698
  2140
qualified lemma compute_floor_fl[code]:
hoelzl@47601
  2141
  "floor_fl (Float m e) = (if 0 \<le> e then Float m e else Float (m div (2 ^ (nat (-e)))) 0)"
lp15@61609
  2142
  apply transfer
lp15@61609
  2143
  apply (simp add: powr_int floor_divide_of_int_eq)
wenzelm@61942
  2144
  apply (metis (no_types, hide_lams)floor_divide_of_int_eq of_int_numeral of_int_power of_int_mult)
wenzelm@61942
  2145
  done
wenzelm@60698
  2146
wenzelm@60698
  2147
end
obua@16782
  2148
lp15@61609
  2149
lemma floor_fl: "real_of_float (floor_fl x) \<le> real_of_float x"
wenzelm@60698
  2150
  by transfer simp
hoelzl@47600
  2151
lp15@61609
  2152
lemma int_floor_fl: "real_of_int (int_floor_fl x) \<le> real_of_float x"
wenzelm@60698
  2153
  by transfer simp
hoelzl@29804
  2154
hoelzl@47599
  2155
lemma floor_pos_exp: "exponent (floor_fl x) \<ge> 0"
immler@67573
  2156
proof (cases "floor_fl x = 0")
wenzelm@53381
  2157
  case True
wenzelm@60698
  2158
  then show ?thesis
wenzelm@60698
  2159
    by (simp add: floor_fl_def)
wenzelm@53381
  2160
next
wenzelm@53381
  2161
  case False
lp15@61609
  2162
  have eq: "floor_fl x = Float \<lfloor>real_of_float x\<rfloor> 0"
wenzelm@60698
  2163
    by transfer simp
lp15@61609
  2164
  obtain i where "\<lfloor>real_of_float x\<rfloor> = mantissa (floor_fl x) * 2 ^ i" "0 = exponent (floor_fl x) - int i"
immler@67573
  2165
    by (rule denormalize_shift[OF eq False])
wenzelm@60698
  2166
  then show ?thesis
wenzelm@60698
  2167
    by simp
wenzelm@53381
  2168
qed
obua@16782
  2169
immler@58985
  2170
lemma compute_mantissa[code]:
wenzelm@60698
  2171
  "mantissa (Float m e) =
wenzelm@60698
  2172
    (if m = 0 then 0 else if 2 dvd m then mantissa (normfloat (Float m e)) else m)"
immler@67573
  2173
  by (auto simp: mantissa_float Float.abs_eq zero_float_def[symmetric])
immler@58985
  2174
immler@58985
  2175
lemma compute_exponent[code]:
wenzelm@60698
  2176
  "exponent (Float m e) =
wenzelm@60698
  2177
    (if m = 0 then 0 else if 2 dvd m then exponent (normfloat (Float m e)) else e)"
immler@67573
  2178
  by (auto simp: exponent_float Float.abs_eq zero_float_def[symmetric])
immler@67573
  2179
immler@67573
  2180
lifting_update Float.float.lifting
immler@67573
  2181
lifting_forget Float.float.lifting
immler@58985
  2182
obua@16782
  2183
end