Utilities for decimal numbers #
Decimal.val commutes with negation for ℚ
Print Decimal exactly in Decimal notation
Equations
- One or more equations did not get rendered due to their size.
Exactly rounded precision reduction #
Exactly rounded conversion from binary exponents to decimal #
We start with n * 2^s and want to convert it to m * 10^t, rounding as desired. Say we're
rounding down, and want to achieve precision prec:
m * 10^t ≤ n * 2^s ∧ m < 10^(prec+1)
m = ⌊n * 2^s / 10^t⌋ ∧ m < 10^(prec+1)
m = ⌊n * 2^s / 10^t⌋ ∧ n * 2^s / 10^t < 10^(prec+1)
m = ⌊n * 2^s / 10^t⌋ ∧ t = ⌈logb 10 (n * 2^s / 10^(prec+1))⌉
We'd like to compute the above without ever blowing up, say by computing a very large n * 2^s
value. We can estimate t using approximate arithmetic, then shift it up or down a bit to get the
minimal value that works. A t value works if
f t = round (n * 2^s / 10^t)
satisfies
f t < 10^(prec+1)
Meh, doing it without blowup seems involved: it . Let's just eat the blowup for now.
Equations
- Decimal.OfBinary.log_10_2_num = 3010299956639812
Instances For
Equations
- Decimal.OfBinary.log_10_2_den = 10000000000000000
Instances For
We underestimate t, since we'll then increase it until we fit within prec
Equations
- Decimal.OfBinary.t_underestimate n s prec = ↑(Nat.log 10 n) + s * ↑Decimal.OfBinary.log_10_2_num / ↑Decimal.OfBinary.log_10_2_den - ↑prec - 2
Instances For
ofBinaryNat rounds down if desired
ofBinaryNat rounds up if desired
Comparison #
For now we very lazily convert to ℚ, even though this can be very inefficient.
Equations
- One or more equations did not get rendered due to their size.
Exact arithmetic #
Decimal.add is exact
Decimal intervals and balls #
Equations
- Decimal.instZeroInterval = { zero := { lo := 0, hi := 0, le := Decimal.instZeroInterval._proof_1 } }
Equations
- Decimal.instZeroBall = { zero := { c := 0, r := 0, r0 := Decimal.instZeroBall._proof_1 } }
Equations
- One or more equations did not get rendered due to their size.