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