Calculus I for Programmers
Did the fee register?
A payments system keeps a running total as a double and adds charges to it one at a time. Some of those charges are extremely small, and a double only carries about sixteen significant digits, so a small enough charge added to a large enough total changes nothing at all: the sum rounds straight back to the number you started with.
Write a function called registers that takes the current total and a fee, and returns true if adding that fee to that total actually changes the stored number, and false if the addition is rounded away. This is the audit tool that catches money quietly vanishing.
You are not being asked to work out the sixteen digit rule yourself. Add the fee to the total and compare the result against the original total. Whether the two are equal is the whole answer.
Input: total = 1.0, fee = 1e-15
Output: true
1.0 + 0.000000000000001 needs sixteen significant digits,
which is just inside what a double holds, so the sum differs from 1.0.Input: total = 1.0, fee = 1e-16
Output: false
This sum needs seventeen significant digits. It is rounded
to the nearest value the format can hold, which is 1.0 again.total is between 0 and 1e16.fee is between 0 and 1000, and may be extremely small.Run your code to see how it does on the examples. Submit also runs the hidden cases.