code wiki / _hdl_build / nx_compound_interest.nx

nx_compound_interest.nx source

↩ module page · 83 lines · 4991 B

1// nx_compound_interest.nx -- the COMPOUND INTEREST tool (X-FIN-COMP-001, financial 2// literacy = the Moroni horns). The operator's keystone: the math the predatory system 3// profits from people NOT understanding. Shows the DUAL NATURE honestly -- the SAME 4// exponential builds wealth FOR you (savings/investing) and buries you AGAINST you (debt) 5// -- so a person SEES it and can defend themselves. Exact integer math (cents + basis- 6// point rates, period-by-period), no f64 rounding games; no-overclaim (real numbers, not 7// get-rich-quick). compound(principal_cents, rate_bps_per_period, periods) -> final cents. 8// Durable: COMPOUND rows -> knowledge/status/compound.log. Exit 0 = KATs hold. 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11const K_MAGIC_5000: i64 = 5000 12const K_MAGIC_10000: i64 = 10000 13const K_MAGIC_100000: i64 = 100000 14const K_MAGIC_162800: i64 = 162800 15const K_MAGIC_162950: i64 = 162950 16const K_MAGIC_500000: i64 = 500000 17const K_MAGIC_20000: i64 = 20000 18func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19func _fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 20func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 21// compound period-by-period: balance += balance * rate_bps / 10000, rounded to nearest cent. 22// optional per-period contribution (cents) added AFTER interest (the savings drip). 23func ci_compound(principal: i64, rate_bps: i64, periods: i64, contrib: i64) -> i64 { 24 var bal: i64 = principal 25 var p: i64 = 0 26 while p < periods { 27 // interest = bal * rate_bps / 10000, round-to-nearest (+5000 before /10000) 28 let interest: i64 = (bal * rate_bps + K_MAGIC_5000) / K_MAGIC_10000 29 bal = bal + interest + contrib 30 p = p + 1 31 } 32 return bal 33} 34// total contributed (principal + all drips) -- the "what you put in" baseline 35func ci_contributed(principal: i64, periods: i64, contrib: i64) -> i64 { 36 return principal + contrib * periods 37} 38func ci_dollars(fd: i64, cents: i64) -> i64 { 39 _fp(fd, "$" as *u8); _fn(fd, cents / 100); _fp(fd, "." as *u8) 40 let c: i64 = cents % 100 41 if c < 10 { _fp(fd, "0" as *u8) } 42 _fn(fd, c) 43 return 0 44} 45func main() -> i64 { 46 _p("=== COMPOUND INTEREST: the same exponential, FOR you and AGAINST you ===\n" as *u8) 47 let lfd: i64 = sys_openat_append("knowledge/status/compound.log" as *u8, 0x1a4) 48 if lfd < 0 { _p(" compound log open failed\n" as *u8); sys_exit(1); return 1 } 49 var bad: i64 = 0 50 // KAT1: $1000 @ 5%/yr for 10yr, no contrib = $1628.89 (1628.894...) -> 162889 cents 51 let k1: i64 = ci_compound(K_MAGIC_100000, 500, 10, 0) 52 if k1 < K_MAGIC_162800 { bad = bad + 1 } 53 if k1 > K_MAGIC_162950 { bad = bad + 1 } 54 // KAT2: $1000 @ 0% = $1000 (no growth, exact) 55 let k2: i64 = ci_compound(K_MAGIC_100000, 0, 10, 0) 56 if k2 != K_MAGIC_100000 { bad = bad + 1 } 57 // KAT3: monotonic -- more periods = more (positive rate) 58 let k3a: i64 = ci_compound(K_MAGIC_100000, 500, 5, 0) 59 let k3b: i64 = ci_compound(K_MAGIC_100000, 500, 20, 0) 60 if k3b <= k3a { bad = bad + 1 } 61 // --- THE DUAL-NATURE DEMONSTRATION (the literacy point) --- 62 // FOR you: $5000 saved + $200/mo @ 7%/yr (~0.565%/mo = 56 bps) for 30yr (360 mo) 63 let savings: i64 = ci_compound(K_MAGIC_500000, 56, 360, K_MAGIC_20000) 64 let saved_in: i64 = ci_contributed(K_MAGIC_500000, 360, K_MAGIC_20000) 65 // AGAINST you: $5000 credit-card debt @ 22% APR (~1.833%/mo = 183 bps), NO payments, 5yr (60 mo) 66 let debt: i64 = ci_compound(K_MAGIC_500000, 183, 60, 0) 67 _fp(lfd, "COMPOUND-FOR-YOU saved_in=" as *u8); ci_dollars(lfd, saved_in) 68 _fp(lfd, " grew_to=" as *u8); ci_dollars(lfd, savings) 69 _fp(lfd, " (30yr @7pct, $200/mo)\n" as *u8) 70 _fp(lfd, "COMPOUND-AGAINST-YOU owed=" as *u8); ci_dollars(lfd, K_MAGIC_500000) 71 _fp(lfd, " became=" as *u8); ci_dollars(lfd, debt) 72 _fp(lfd, " (5yr @22pct APR, no payments -- the SAME math, against you)\n" as *u8) 73 _p(" FOR you: put in "); ci_dollars(1, saved_in); _p(" -> grew to "); ci_dollars(1, savings); _p(" (30yr @7%, $200/mo)\n" as *u8) 74 _p(" AGAINST you: owed "); ci_dollars(1, K_MAGIC_500000); _p(" -> became "); ci_dollars(1, debt); _p(" (5yr @22%, no payments)\n" as *u8) 75 _fp(lfd, "COMPOUND-GATE epoch=" as *u8); _fn(lfd, sys_now_realtime_sec()) 76 _fp(lfd, " kats=" as *u8); _fn(lfd, 4 - bad) 77 if bad == 0 { _fp(lfd, "/4 verdict=GREEN\n" as *u8) } else { _fp(lfd, "/4 verdict=RED\n" as *u8) } 78 sys_close(lfd) 79 if bad == 0 { _p(" COMPOUND: GREEN (exact, honest -- the math people are not taught)\n" as *u8); sys_exit(0); return 0 } 80 _p(" COMPOUND: RED\n" as *u8) 81 sys_exit(1) 82 return 1 83}