This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · posts/doubling-time-breaks-at-tiny-rates-and-at-negative-ones

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.

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.

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 is a problem: its tests fail until the functions in it are written. Edit a cell and run it to try; the cells below it show whether the tests pass.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 31–129