1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! # Doubling rules: Rule of 72 / 70 / 69 vs exact math
//!
//! Teaching example for the [`finance_solution::returns`] doubling-time APIs.
//! Read this file **without running it** and you should still see every important
//! number and table the program prints — the same documentation style as
//! [`common_word_problems`](../common_word_problems.rs).
//!
//! ## How to run
//!
//! ```bash
//! cargo run --example doubling_rules
//! ```
//!
//! ## What this example covers
//!
//! 1. **Word problem (8%)** — “About how long to double?” with Rule of 72 vs exact.
//! 2. **Solution struct** — formulas + method comparison table at one rate.
//! 3. **Scalar helpers** — `rule_of_72` / `rule_of_70` / `rule_of_69` / `doubling_time` /
//! `doubling_time_continuous` side by side.
//! 4. **Multi-rate table** — 1%…15%: when is Rule of 72 “good enough”?
//! 5. **Symmetry check** — $1 grown for exact discrete periods ≈ $2.
//! 6. **Error path** — zero / negative rates return `FinanceError`, not a panic.
//!
//! ---
//!
//! ## Sample run (full terminal output)
//!
//! ### Problem 1 — single rate 8%
//!
//! ```text
//! === Problem 1: How long to double at 8%? ===
//!
//! Word problem:
//! You earn a steady 8% per year. Roughly how many years until money doubles?
//! Rule-of-thumb answer (Rule of 72): 72 / 8 = 9 years.
//!
//! solution formulas:
//! exact 9.0065 = ln(2) / ln(1 + 0.080000); rule_72 9.0000 = 72 / (8.0000)
//! exact = ln(2)/ln(1+r); rule_72 = 72/(100*r); rule_70 = 70/(100*r); rule_69 = 69/(100*r); continuous = ln(2)/r
//!
//! rule_of_72: 9.0000 years (error vs exact: -0.0065)
//! rule_of_70: 8.7500 years
//! rule_of_69: 8.6250 years
//! exact_discrete: 9.0065 years ← ln(2)/ln(1+r)
//! exact_continuous: 8.6643 years ← ln(2)/r (slightly faster)
//!
//! method comparison table (s.print_table()):
//!
//! method years error_vs_exact
//! ---------------- ------ --------------
//! rule_of_72 9.0000 -0.0065
//! rule_of_70 8.7500 -0.2565
//! rule_of_69 8.6250 -0.3815
//! exact_discrete 9.0065 0.0000
//! exact_continuous 8.6643 -0.3421
//! ```
//!
//! Teaching note: at 8%, Rule of 72 is almost exact (error only about 0.0065 years).
//!
//! ### Problem 2 — multi-rate comparison 1%…15%
//!
//! ```text
//! === Problem 2: When is Rule of 72 good enough? (1% … 15%) ===
//!
//! rate rule_72 rule_70 rule_69 exact continuous err_72
//! -------- ------- ------- ------- ------- ---------- -------
//! 0.010000 72.0000 70.0000 69.0000 69.6607 69.3147 2.3393
//! 0.020000 36.0000 35.0000 34.5000 35.0028 34.6574 0.9972
//! 0.030000 24.0000 23.3333 23.0000 23.4498 23.1049 0.5502
//! 0.040000 18.0000 17.5000 17.2500 17.6730 17.3287 0.3270
//! 0.050000 14.4000 14.0000 13.8000 14.2067 13.8629 0.1933
//! 0.060000 12.0000 11.6667 11.5000 11.8957 11.5525 0.1043
//! 0.070000 10.2857 10.0000 9.8571 10.2448 9.9021 0.0409
//! 0.080000 9.0000 8.7500 8.6250 9.0065 8.6643 -0.0065
//! 0.090000 8.0000 7.7778 7.6667 8.0432 7.7016 -0.0432
//! 0.100000 7.2000 7.0000 6.9000 7.2725 6.9315 -0.0725
//! 0.110000 6.5455 6.3636 6.2727 6.6419 6.3013 -0.0964
//! 0.120000 6.0000 5.8333 5.7500 6.1163 5.7762 -0.1163
//! 0.130000 5.5385 5.3846 5.3077 5.6714 5.3319 -0.1330
//! 0.140000 5.1429 5.0000 4.9286 5.2901 4.9511 -0.1472
//! 0.150000 4.8000 4.6667 4.6000 4.9595 4.6210 -0.1595
//! ```
//!
//! Teaching note: Rule of 72 **overstates** years at low rates (err_72 > 0 below ~8%)
//! and **understates** slightly above ~8%. Best “quick mental math” near 6%–10%.
//!
//! ### Problem 3 — symmetry at 8%
//!
//! ```text
//! === Problem 3: Symmetry — grow $1 for exact discrete periods ===
//!
//! (1 + 0.08)^exact = 2.0000000000 (expect 2)
//! ```
//!
//! ### Problem 4 — invalid rates (Result-only API)
//!
//! ```text
//! === Problem 4: Invalid rates are FinanceError, not panics ===
//!
//! rule_of_72(0.0) → Err(ZeroValue { field: "rate" })
//! doubling_time(-0.05) → Err(InvalidRate { rate: -0.05 })
//! doubling_compare_rates(&[]) → Err(Unsolvable {
//! message: "doubling_compare_rates requires at least one rate" })
//! ```
//!
//! ---
//!
//! Related APIs: [`doubling_solution`], [`doubling_compare_rates`], [`rule_of_72`],
//! [`doubling_time`], [`doubling_time_continuous`].
use *;
// ---------------------------------------------------------------------------
// Problem 1
// ---------------------------------------------------------------------------
// Word problem:
// You can invest at a steady 8% per year, compounded once per year.
// Roughly how many years until your money doubles?
//
// Mental math (Rule of 72): 72 / 8 = 9 years.
// Exact discrete: ln(2) / ln(1.08) ≈ 9.0065 years.
// Expect Rule of 72 ≈ exact at this rate (error about −0.0065 years).
//
// Sample solution table is in the //! module docs above.
// ---------------------------------------------------------------------------
// Problem 2
// ---------------------------------------------------------------------------
// Word problem / teaching question:
// Across common annual rates (1% … 15%), when is Rule of 72 a good mental
// approximation of the true (discrete) doubling time?
//
// Expect:
// - Low rates (1%–5%): Rule of 72 overstates years (err_72 positive, up to ~2+ years at 1%).
// - Around 8%: error near zero (Rule of 72 is calibrated near here).
// - Higher rates (10%–15%): Rule of 72 slightly understates years (err_72 negative).
//
// Full multi-rate table is in the //! module docs above.
// ---------------------------------------------------------------------------
// Problem 3
// ---------------------------------------------------------------------------
// Symmetry / sanity check:
// If exact discrete doubling time is T = ln(2)/ln(1+r), then
// (1 + r)^T must equal 2 (within floating-point noise).
//
// Expect: (1.08)^9.0065… = 2.0000000000
// ---------------------------------------------------------------------------
// Problem 4
// ---------------------------------------------------------------------------
// Error-handling problem (v0.1 Result-only API):
// What happens if a student plugs in 0% or a negative rate?
//
// Expect: FinanceError variants — never a panic on domain input.
// rule_of_72(0.0) → Err(ZeroValue { field: "rate" })
// doubling_time(-0.05) → Err(InvalidRate { rate: -0.05 })
// doubling_compare_rates(&[]) → Err(Unsolvable { … "one rate" … })