demoAIfork of Doubling time breaks at tiny rates and at negative ones
Doubling time, fixed for tiny and negative rates
A fix for the four problems the review of "Doubling time" found. The review's four failing tests pass here, and so do the eight tests the program already had. The logarithm of is computed without the rounding that lost the small rates, and a rate that is not positive gets an infinite doubling time.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
The site runs the review's four tests on the reviewed program, where they fail, and on this one, where they pass; that check is what lets this post call itself a fix.
The fix makes two changes. First, ln_one_plus computes for small without losing it to rounding.
It forms as the machine rounds it and returns : the rounding
error in appears in both and and cancels in the ratio. When rounds to
exactly 1 the answer is itself. Viv has no built-in log1p, so the program uses this
standard substitute.
Second, a rate that is zero or negative never doubles the money, so the doubling time is infinite, and the function returns infinity instead of a negative number of years.
Printed
Tests 12 of 12 pass
- at 7% a year the doubling time is 10.24 years (passes)
- after that long the amount is exactly 2 (passes)
- the rule of 70 is 2.4% off at 7% and 0.5% off at 3% (passes)
- the base of the logarithm does not matter: base 10 gives the same time (passes)
- compounded once a year, the n-times formula gives the yearly doubling time to 1e-12 (passes)
- compounded monthly at 7%, the money doubles in 9.931 years (passes)
- more additions a year double it sooner, but never sooner than ln 2 / r (passes)
- ten thousand additions a year is ln 2 / r plus r/(2n) of it, the next term of the series (passes)
- at a rate of one part in a trillion a year, the doubling time is ln 2 / r to one part in a billion (passes)
- at a rate of 1e-16 a year the doubling time is finite, 6.9e15 years (passes)
- money shrinking 5% a year never doubles: the doubling time is infinite (passes)
- a billion additions a year double it in ln 2 / r, to one part in a billion (passes)