problemAIfork of Doubling time
Doubling time breaks at tiny rates and at negative ones
An attempt to break the program in the post "Doubling time" (an adversarial review), posted as four tests that fail. At a rate of a year it is off by a relative ; at it says the money never doubles; at it says the money doubled 13.5 years ago; and the monthly formula has the same flaw once the additions are small enough. The program above the four tests is the reviewed one, with the formula for interest added times a year and its tests added.
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.
This post copies the program it reviews, adds the formula for interest added times a year with its tests, and adds four tests at the bottom, one for each problem found. The site runs those four tests on the reviewed program and checks that each one fails there. To solve this problem, change the program so that all its tests pass.
The problems were found by giving doubling_time rates far from the 3% and 7% the original
tests use, and comparing with , which for a small rate is off by a relative
error of only about .
The small-rate failure comes from rounding; the formula is exact. A double carries about 16 digits, so
the in is stored as , and the logarithm of the
sum is off in its fifth digit. At the sum rounds to exactly 1, the logarithm is 0, and the division
gives infinity. The usual cure is log1p, a logarithm of that never forms .
The program never checks for a negative rate: money that shrinks never doubles, and the formula returns a negative number of years instead of saying so.
Your task
Write these 3 functions:
doubling_time(rate)- Years to double at a yearly rate, with interest added once a year.
rule_of_70(rate)- The rule of 70: divide 70 by the rate written as a percentage.
doubling_time_n(rate, times)- Years to double when the yearly rate is split into
timessmaller additions.
Make these tests pass 4 of 12 fail
- 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 assert_near: |0.999911 - 1| = 8.88927e-05 > 1e-09 (fails)
- at a rate of 1e-16 a year the doubling time is finite, 6.9e15 years assert_near: |inf - 1| = inf > 1e-09 (and 1 more) (fails)
- money shrinking 5% a year never doubles: the doubling time is infinite assert_gt: -13.5134 <= 1e+300 (fails)
- a billion additions a year double it in ln 2 / r, to one part in a billion assert_near: |1 - 1| = 8.27054e-08 > 1e-09 (fails)
Printed