Exercise · Volume 1 · 1.2.4-5
Exercise 1.2.4-5
proved in Lean solution written
| Rating | 16 |
|---|---|
| Kind | K3, find a value: compute a number, a set or a closed form from the written solution |
| Section | Section 1.2.4 of Volume 1. Companion chapter: Integer functions and elementary number theory |
The task
Find one formula that rounds a positive real to the nearest integer, with a half going up, and say what the formula does for negative .
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Write with .
Hint 2
Add to before you take a floor.
Solution
Show the solution
The formula is . Write with . Then , and is an integer, so .
If , then and the last floor is . The result is .
If , then and the last floor is . The result is . In this case , so is not an integer and . The result is .
This is the required rule. The calculation did not use the sign of , so the formula is valid for negative too, and it still gives an integer nearest to . The only difference is the treatment of the halves: has and the formula gives , which is the half rounded up toward , not away from zero. A rounding that is symmetric about zero is . The formula rounds every half down.
Check
For and and the formula gives , , . For , , it gives , , . A computer run with exact fractions agrees for with .
Remarks
Rounding the half to even is the rule of many floating-point units, to avoid a bias. The formula here has a bias of per tie. See Section 4.2.2 for rounding in floating-point arithmetic.
This solution is checked, and a person has not reviewed it yet. The source.
In Lean
proved The statement and its proof are in Lean. The kernel accepts the proof, and it uses the standard axioms only.
- Problem:
TAOCP.S1_2_4.Ex5.statementaxioms: Classical.choice, Quot.sound, propext - Solution:
TAOCP.S1_2_4.Ex5.solvedaxioms: Classical.choice, Quot.sound, propext
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.