PC-1211, PC-1250, etc. TVM
|
06-05-2024, 12:34 PM
Post: #21
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
To make the post more clearly, I quoted the code used here
(06-05-2024 01:05 AM)Albert Chan Wrote: 10 INPUT "B,N,P,M,F? ";B,N,P,M,F @ B=B*M Since Sharp does not have EXPM1, LOGP1, lets try without them. For good measure, we also report the next iteration. >20 DEF FNF(I) @ S=1-(1+I)^(-N) >70 DISP (I-Y/D)*100 >run B,N,P,M,F? 0,360,0,-1000,1e6 .313028391431 504.47069099 .471270710728 62.96905505 .497374290313 1.51995026 .498036167469 .00095736 .498036584886 -.0000025 .498036583796 Final I is an improvement ... but it is really luck. Garbage in, Garbage out. More Newton iterations suggested we only have 8 to 9 digits accuracy. .498036583796 .498036583626 .498036583308 .49803658459 .498036583243 ... One way is to correct for S (1+I) error. (1+I)^(-n) = (1+I+ε)^(-n) * (1 + ε/(1+I)) ^n ≈ (1+I+ε)^(-n) * (1 + n*ε/(1+I)) >20 DEF FNF(I) @ X=1+I @ S=X^(-N) @ S=1-S-N*(X-1-I)/X*S >run B,N,P,M,F? 0,360,0,-1000,1e6 .313028391431 504.47068755 .471270710304 62.96905734 .497374290775 1.51994785 .498036166884 .00095668 .498036584005 .00000001 .498036584009 Much better, but S calculation is messy. A novel way is to set ε=0 !!! >20 DEF FNF(I) @ S=1-(1+I)^(-N) >50 H=-Y/D @ I=I+H+1-1 @ Y=FNF(I) ! Newton's method >run B,N,P,M,F? 0,360,0,-1000,1e6 .313028391 504.47068892 .47127071 62.96905807 .497374291 1.51994733 .498036167 .00095642 .498036584 .00000002 .498036584009 |
|||
06-05-2024, 07:16 PM
(This post was last modified: 07-11-2024 07:51 PM by robve.)
Post: #22
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-05-2024 12:34 PM)Albert Chan Wrote: Since Sharp does not have EXPM1, LOGP1, lets try without them. Thanks, this is more clear. EXPM1 LNP1 are easy to implement to compute TVM more accurately using a few terms of their series when applicable (up to 4th term of the series with 10 digits precision since 1+0.01^4/4 is close to 1 no need to use 5th term): ' compute L=LN(1+J) and S=(1+J)^-N and R=S-1 40 J=.01*I,K=1+J*B,C=0 : IF ABS J>=.01 LET L=LN(1+J) : GOTO 42 41 L=J*J,L=J-L/2+J*L/3-L*L/4 42 S=-N*L : IF ABS S>=.01 LET S=EXP S,R=S-1 : RETURN 43 R=S*S,R=S+R/2-S*R/6+R*R/24,S=R+1 : RETURN With this, the secant method works quite well and is quick, small and simple. We just need to replace LN(1+J) with L and use the improved S and R: Code: 10 "B" B=B=0 : PRINT "BGN=";MID$("NY",B+1,1) : END Line 32 is added to avoid the initial secant slope to be zero in extreme cases when the rate is very small, such as in examples 3,4,5 below. The program always terminates with a result or gives an error (not likely, but could be in extreme and unrealistic TVM cases). When G counts down to zero from 9, the result is likely not correct because a root wasn't reliably found (not likely, but can happen in extreme TVM problems). 1) B=1,N=40,PV=900,FV=-1000,PMT=-40 comp I% => 4.753367189% is exact 2) B=1,N=40,PV=900,FV=-1000,PMT=-400 comp I% => 80% is exact in 10 digits, but 80.0000000009845% comp FV => -1462.657745 (should be -1000, but TI BA gives -900(*) and Sharp EL-735 gives -975.11 and HP prime gives -1001.6 so it's tricky to compute FV accurately) 3) from http://www.voidware.com/tvm.htm B=0,N=365*24*60*60,PV=0,PMT=-0.01,I%=10/N, comp FV => 331667.0067 is exact comp I% => 3.170979194E-07 is almost exact 3.170979198E-07 except for the last digit is 4 instead of 8 4) same as 3 but take N=365*24*60 and I%=10/N: comp FV => 5527.782898 is exact comp I% => 1.902587519E-05 is exact 5) same as 3 but take N=365*24*60 and I%=10/60/N: comp FV => 5260.377543 is exact comp I% => 3.170979051E-07 is very close to exact 3.170979198E-07 6) from http://www.voidware.com/tvm.htm (the hard example 2) B=0,N=32,PV=-999999,FV=1000000,PMT=0 comp I% => 3.125027014E-06 is very close to exact 3.125E-06 7) B=0,N=360,PV=0,PMT=-1000,FV=1000000 comp I% => 4.980365842E-01 is exact (note that this is 5.976% APR) EDIT: as a sanity check, I verified this program with 27 TVM problems with solutions posted by a university finance department's lectures. To use this program on a Sharp pocket computer: - Switch begin mode on/off: DEF-B - Enter values: DEF-N or DEF-J (I%) or DEF-V (PV) or DEF-M (PMT) or DEF-F (FV) - Calculate: press DEF-C (beep) then DEF-N or DEF-J or DEF-V or DEF-M or DEF-F EDIT 2: once the N, PV, PMT, FV and I% are entered or computed and BGN is on/off, you can obtain amortization by adding five lines to the program: Code: 50 "A" AREAD A : J=.01*I,R=P-F,T=0,V=0 Enter a period value then press DEF-A. When the interest paid and principle paid values are shown, press ENTER again to show the next period and so on. The values T, V and R are the accumulated interest, accumulated principal and remaining balance. It should be easy to amend the program for direct P1/P2 and ACC computations if so desired. EDIT 3: updated line 30 with I=1 to improve interest rate computation accuracy - Rob "I count on old friends to remain rational" |
|||
06-05-2024, 10:06 PM
Post: #23
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-05-2024 07:16 PM)robve Wrote: but can happen in extreme TVM problemsI'm sure you're aware and everyone's bored to death of my requests and updates on this collation of tricky TVM problems and results. If you feel that one of these TVM problems can highlight a particular deficiency I might be tempted to add it in, and of course curious how your different algorithms and approaches fare. |
|||
06-06-2024, 12:27 AM
(This post was last modified: 06-06-2024 02:24 PM by Albert Chan.)
Post: #24
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
Hi, robve
If you have good EXP, LN, you can build accurate EXPM1 LNP1 (02-04-2019 07:28 PM)Dieters formula Wrote: expm1(x) = (u-1) - (ln(u) - x) * u, where u = exp(x), rounded And, many thanks to your examples. I actually discovered a Plus42 bug, for rate very close to 0%. Code: 10 INPUT "B,N,P,M,F? ";B,N,P,M,F @ B=B*M With FNF(I ≈ 0) added, updated version handled all your examples. Some F values adjusted to true 10 digits accuracy, to test for I round-trip Code: >run ! 1 Update: Code showed initial edge rate as well (previously hidden) |
|||
06-06-2024, 11:02 PM
Post: #25
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-05-2024 07:16 PM)robve Wrote: 2) B=1,N=40,PV=900,FV=-1000,PMT=-400 This is a really curious example! If you have FV = -100,000 what do you get for I%? If you start receiving 900, immediately paying out 400, then you have 500. 80% of 500 is 400 interest, which means the balance is 900 at the next payment period, when you are paying out 400. Ad infinitum presumably. You're paying off your interest, so it's odd that these calculators can return -1000 for FV. It's also odd they can return you an interest rate no matter how large FV is, yet the FV can never get more than -900. It's also quite fun to solve for N with PV = 900, PMT = -400, FV = 900. On my DM42 solver I get a div/0 error, on plus42 it correctly tells me data is invalid. But then put in FV = 1000. Plus42 returns and error, but my solver gives me a negative number. Confused. |
|||
06-06-2024, 11:34 PM
Post: #26
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-06-2024 12:27 AM)Albert Chan Wrote: And, many thanks to your examples. Me too! (not that surprising). Is this re: example 5 above? My code gets stuck with f and f0 dancing around zero. Maybe we should start a thread about this? In R it only seems to happen when precision is turn up beyond 250ish. |
|||
06-07-2024, 12:27 AM
(This post was last modified: 06-07-2024 01:51 AM by Albert Chan.)
Post: #27
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-06-2024 11:02 PM)dm319 Wrote: You're paying off your interest, so it's odd that these calculators can return -1000 for FV. That's because robve other calculators do not have rate exactly 80% lua> tvm_begin(40, nil, 900, -400, -1000) 0.8000000000098451 lua> tvm_begin(40, _, 900, -400, nil) -1000.0000635560003 Quote:If you have FV = -100,000 what do you get for I%? lua> tvm_begin(40, nil, 900, -400, -1e5) 0.8000000097564842 lua> tvm_begin(40, _, 900, -400, nil) -100000.00038198779 |
|||
06-07-2024, 01:45 AM
(This post was last modified: 06-18-2024 09:52 AM by Albert Chan.)
Post: #28
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-06-2024 11:34 PM)dm319 Wrote:(06-06-2024 12:27 AM)Albert Chan Wrote: And, many thanks to your examples. Yes, code had issue with example 5 (close to 0% bug). Plus42 should have this fixed soon. (issue sent) Plus42 previous version, secant's method did hang for some cases. Now, with true Newton's method and edge guess, this issue goes away. Here is closer to Plus42 do_i_pct_yr(), termination criteria based from Y, not I It does not relied on quadratic convergence, but finite ULP's: ULP(FNF) ≥ ULP(M) Rate search will not get stuck, even if no solution exist. 10 INPUT "B,N,P,M,F? ";B,N,P,M,F 12 IF B THEN P=P+M @ F=F-M ! end mode 14 IF ABS(P)>ABS(F) THEN X=F @ F=-P @ P=-X @ N=-N ! time reversed 20 DEF FNZ(I,A,B,C) @ D=(A+B)/2 @ B=A*C @ FNZ=A+M+(D+B)*I @ D=D+2*B @ END DEF 30 DEF FNF(I) @ IF 1+N*I*I=1 THEN FNF=FNZ(I,(P+F)/N,P-F,(N*N-1)/12*I) @ END 40 S=EXPM1(LOGP1(I)*N) @ D=(P+F)/S*(1-N*(S+1)/(S+S/I))+P 50 FNF=((P+F)/S+P)*I+M @ END DEF 60 I=M/F @ Y=FNF(I) ! "smallest" edge guess 70 DISP 100*I,Y @ H=-Y/D @ I=I+H @ Y0=Y @ Y=FNF(I) 80 IF SGN(Y0)=SGN(Y) AND ABS(Y0)>ABS(Y) THEN 70 90 DISP 100*I,Y @ J=I-Y/D/2 @ DISP 100*J ! half-correction Code: >run ! 1 Update: 1. Small rate branch test (1+I=1) --> (1+N*I*I=1) Small rate branch does quadratic fit, not suitable for huge compounding effect. 2. time-symmetry to keep size of P small, possibly less cancellation error. With this change, "smallest" edge rate is simply M/F 3. Final Newton correction cut in half, to reduce error radius. |
|||
06-07-2024, 02:30 AM
(This post was last modified: 06-08-2024 08:10 PM by robve.)
Post: #29
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-06-2024 12:27 AM)Albert Chan Wrote: If you have good EXP, LN, you can build accurate EXPM1 LNP1 Like this cryptic line to compute L=LN(1+J) and S=(1+J)^-N and R=S-1: Code: 40 J=.01*I,K=1+J*B,C=0,L=1+J,L=LN L-(L-1-J)/L,S=-N*L,R=EXP S,R=R-1-(LN R-S)*R,S=R+1 : RETURN Replace line 40 to 43 with the above line. With Dieters formulas there are a few minor changes for the more extreme examples, while the other examples and the 27 TVM problems I tested remain unaffected: 2) comp FV => -900 (which is closer to -1000 and the same result as TI BA, but is a tricky one) 4) comp FV=5527.782902 (tiny bit better in the very last digit +/- 3) comp I%=1.902587522E-05 (slightly better in the last 2 digits) 5) comp FV=5260.382453 (slightly better in the last 2 digits) comp I%=3.170988253E-07 (somewhat worse, but in both cases only first 5 digits are precise and this result just differs by more) 6) comp I%=3.1248997E-06 (somewhat worse, but in both cases only the first 3 digits are precise and this result just differs by more) It is a bit mixed pro/cons perhaps, but I do like the improved FV accuracy. EDIT: I should mention that SHARP PC round to 10 digits when storing values in variables. But internally 12 digits are used. This matters when calculating EXPM1 and LNP1. EDIT 2: do not use this method to compute EXPM1 and LNP1, because for larger negative S we get R=0 and LN(R) stops with an error. - Rob "I count on old friends to remain rational" |
|||
06-08-2024, 06:19 PM
Post: #30
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-07-2024 12:27 AM)Albert Chan Wrote: lua> tvm_begin(40, nil, 900, -400, -1000) Ah! That makes sense. I was thrown by the 80% exact. I guess this problem is very affected by the number of digits of precision the calculator can store on that interest rate. I added two of the problems to my bank of TVM problems. The HP-80 and HP-70 return -10.7 and -11.0% instead of 0.1666 recurring for problem 5 (Just realised I have measured an annual rate by i x 60x24x365). The early HPs all the way up to the HP12c seem to really struggle with this one. In fact the 12c regresses somewhat, with the 37E and 38E doing a bit better, and the HP-92 doing quite badly. Weirdly for this problem, I thought I was getting issues with the solver (which is the plus42 solver basically), but testing it today it seems to have gone away and I don't quite understand why. It's returning a reasonable result down to 27 decimal points, with plus42 at 30 DP. Problem 2 below is also interesting. The first HP that can do it basically only gets ~900, same for HP-12c. But we get much better results with the HP-20b/HP30b and HP-12c platinum which have more digits of precision. There was a surprise fail with the TI-83+, and it may relate to me taking the answer out, storing in a variable, and putting it back in (which is a PITA). |
|||
06-08-2024, 08:38 PM
Post: #31
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-06-2024 12:27 AM)Albert Chan Wrote: If you have good EXP, LN, you can build accurate EXPM1 LNP1 To follow up on my reply on Albert's suggestion to use this method, I found out that this causes errors in some cases such as when the interest rate is very small and N is large. The problem is that for larger negative S=-N*L we get R=0 and LN(R) quits with an error (or NaN): Code: 40 J=.01*I,K=1+J*B,C=0,L=1+J,L=LN L-(L-1-J)/L,S=-N*L,R=EXP S,R=R-1-(LN R-S)*R,S=R+1 : RETURN I made one other minor change to my code. With this change I am now able to accurately compute interest rates on the SHARPs with only 10 digits BCD and limited range (1E-99 to 9.999999999E+99), even for the more challenging cases: 1) unchanged, is exact 2) unchanged, is exact 3) I%=3.170979194E-07 is almost exact 3.170979198E-07 4) I%=1.902587519E-05 is exact 5) I%=3.170979051E-07 is very close to exact 3.170979198E-07 6) I%=3.125027014E-06 is very close to exact 3.125E-06, but HP-12C also gives an inexact result 3.125004736E-06 7) I%=4.980365842E-01 is exact The change is to replace I=.02 with I=1 on line 30: Code: 30 G=9,I=1 : GOSUB 38 The number of secant method iterations is about the same as before. The number of iterations for the examples are: 6, 5, 4, 14, 12, 10, 3, 7. With secant, the number of function evaluations is just the number of iterations, which is nice. Note that I use a loop on line 32 that finds a suitable starting rate and this may take a few iterations in the worst case. But this is usually not necessary and only happens for cases 3, 4 and 5 that return very low interest rates with very large values of N which is a challenging combination. I will update my previous post to include the updated code and improved results. - Rob "I count on old friends to remain rational" |
|||
06-08-2024, 09:19 PM
Post: #32
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-08-2024 08:38 PM)robve Wrote: To follow up on my reply on Albert's suggestion to use this method, I found out that this causes errors in some cases such as when the interest rate is very small and N is large. The problem is that for larger negative S=-N*L we get R=0 and LN(R) quits with an error (or NaN) Thanks! lua> LOG1P = fn'x,y: y=1+x; log(y) - (y-1-x)/y' lua> EXPM1 = fn'x,y: y=exp(x); y-1 - y*(log(y)-x)' lua> LOG1P(-1), EXPM1(-1e6) -nan -nan Dieter's formula require testing for y==0 edge case. lua> LOG1P = fn'x,y: y=1+x; log(y) - (y==0 and 0 or (y-1-x)/y)' lua> EXPM1 = fn'x,y: y=exp(x); y-1 - (y==0 and 0 or y*(log(y)-x))' lua> LOG1P(-1), EXPM1(-1e6) -inf -1 |
|||
06-08-2024, 09:21 PM
(This post was last modified: 06-08-2024 09:22 PM by robve.)
Post: #33
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-08-2024 06:19 PM)dm319 Wrote:(06-07-2024 12:27 AM)Albert Chan Wrote: lua> tvm_begin(40, nil, 900, -400, -1000) Indeed, 80% is exact in 10 places. I'm working with 10 digits only on the SHARPs. I've added an annotation to my old post to explain. If you want another bewildering TVM problem: B=1 (begin mode) N=40 PV=900 PMT=1000 FV=-1000 What's the interest rate? In the real world this makes no sense, i.e. getting rich for nothing in return, but mathematically it is a valid TVM problem to solve. Try it on a calculator. Is the rate you get a whole number? - Rob "I count on old friends to remain rational" |
|||
06-08-2024, 11:47 PM
(This post was last modified: 06-11-2024 10:59 PM by Albert Chan.)
Post: #34
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-08-2024 09:21 PM)robve Wrote: If you want another bewildering TVM problem: lua> n, pv, pmt, fv = 40, 900, 1000, -1000 lua> pv, fv = pv+pmt, fv-pmt -- Now, b=0 lua> pv, pmt, fv 1900 1000 -2000 Solve with split loan method f(x) = x + (pv+fv) / ((1+i)^n-1), where i = pmt / (x-pv) Code: Loan: n pv pmt fv lua> i = pmt / fv -- "smallest" edge rate = -0.5 With negative i and positive n, ((1+i)^n-1) --> -1, may lose many significant digits So, we flip for time-reversed setup. lua> n, pv, fv = -n, -fv, -pv lua> x = -(pv+fv) / expm1(log1p(i)*n) lua> x -9.094947017737554e-11 i = pmt / (x - pv) = (-pmt/pv) / (1 - (x/pv)) ≈ (-pmt/pv) * (1 + (x/pv)) Since -pv was fv before time-reversal, first term is just old i = -0.5 It also mean old x = 0. No more iteration necessary. lua> i .. ' + ' .. i*(x/pv) -0.5 + 2.2737367544343885e-14 rate (%) = -49.9999999999977262632455656115 Equivalent way, tvm() to get x*i, divide by pv to get i error lua> n,i,pv,pmt,fv -40 -0.5 2000 1000 -1900 lua> tvm(n, i, 0, nil, pv+fv) / pv 2.2737367544343885e-14 |
|||
06-09-2024, 12:06 AM
(This post was last modified: 06-09-2024 12:31 AM by robve.)
Post: #35
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-08-2024 11:47 PM)Albert Chan Wrote:(06-08-2024 09:21 PM)robve Wrote: If you want another bewildering TVM problem:rate (%) = -49.9999999999977262632455656115 Yes, excellent! Indeed -50% is not the exact answer beyond 12 digits. It can't be, given the TVM parameters. So is the rate you get (on a calculator) a whole number? Probably yes, but it fools you. EDIT: With my modified TVM program when written in C using double precision: Code: ./tvm 40 900 1000 -1000 1 The C program: Code: #include <stdio.h> This uses a bunch of global variables for TVM parameters to make it easier to adapt to registers in a calculator. - Rob "I count on old friends to remain rational" |
|||
06-09-2024, 10:12 AM
Post: #36
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
What a delicious problem! (can a TVM problem be delicious?) It's like the runner that goes half the distance to the finish line each second, but never quite gets there...
The earliest calculator I can try it out on is the HP-92, but basically it seems all HPs give the same answer, up to the HP-30b. RPN83P gives a different answer to the native TI-83+, but doesn't quite get another digit of accuracy, though it hints that the answer may not be exactly -50. I ran the problem in R using the rmpfr library to compare with plus42 and the like! Code: | calculator | answer | accuracy | |
|||
06-09-2024, 01:22 PM
Post: #37
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-08-2024 09:21 PM)robve Wrote: [snip] Using an HP-27S, I get -50 exactly. --Bob Prosperi |
|||
06-11-2024, 03:32 PM
(This post was last modified: 06-12-2024 03:38 PM by Albert Chan.)
Post: #38
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-02-2024 11:18 PM)Albert Chan Wrote:(06-02-2024 06:37 PM)Albert Chan Wrote: rate = pmt/(x-pv) I found a more elegant proof, using reciprocal rate r = 1/i = (x-pv)/pmt x2 = x - f / (df/dx) (pmt*r2 + pv) = (pmt*r + pv) - f / (df/dr / pmt) r2 = r - f / (df/dr) Both are equivalent. We just need to show one has same convergence rate as npmt. f(r) = (pmt*r + pv) + (pv+fv) / ((1+1/r)^n-1) Compare this to g = npmt function (terms rearranged to make pattern clearer) g(i) = (pmt + pv*i) + (pv+fv) / ((1+i)^n-1) * i We have f = r*g. Let fp = df/dr, gp = dg/di fp = d(r*g)/dr = (g + r * (dg/di) / (dr/di)) = (g + (1/i) * gp * (-i^2)) = (g - i*gp) Newton iteration for next estimate: r2 = r - f/fp = 1/i - (g/i) / (g - i*gp) = 1 / (i - g/gp) = 1 / i2 QED f(r) = (pmt*r + pv) + (pv+fv) / ((1+1/r)^n-1) Bonus trivia, from recipocal rate formula, f(1/i) = npmt(i)/i n assumed positive. If not, we can use time-symmetry: {n,pv,fv} --> {-n,-fv,-pv} f(r = -1) = limit(npmt(i)/i, i = -1) = (pmt*-1+pv) + (pv+fv)/(∞^-n-1) = (-fv-pmt) f(r = 0) = limit(npmt(i)/i, i = +∞) = (pmt*0+pv) + (pv+fv)/(∞^n-1) = pv For extreme rate, npmt(i) is basically linear, slope = (pv if n>0 else -fv) |
|||
06-11-2024, 07:13 PM
(This post was last modified: 06-12-2024 05:03 PM by robve.)
Post: #39
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
An improved version of the TVM program for SHARPs.
I kept it compact (22 lines of BASIC) and made it faster than the previous version. It takes only a few seconds to compute interest rates (faster than the SHARP EL-735). This version is also fully tested (on a SHARP PC-1403H). I wanted to keep it small while returning accurate results, at least as accurate as possible with a 10 digit BASIC calculator that has limitations. This update uses LNP1 and EXPM1 formulas by Dieter and the initial interest guess by Albert. Also I%=0 is now permitted as input and is returned when the computed interest is zero or very small. This required a couple of tricks to avoid computation errors. There is no error trapping. When a computation fails then the result is ERR. Some explanations are included in the program. How to use: - Switch begin mode on/off: DEF-B - Enter values: DEF-N or DEF-J (I%) or DEF-V (PV) or DEF-M (PMT) or DEF-F (FV) - Calculate: press DEF-C (beep) then DEF-N or DEF-J or DEF-V or DEF-M or DEF-F Code: 10 "B" B=B=0 : PRINT "BGN=";MID$("NY",B+1,1) : END Notes: - line 30: a retry counter G is still necessary on this calculator, but G=3 suffices instead of G=9 with the improved initial guess - line 31: we must avoid initializing I%=0 even when pv+fv+n*pmt is nonzero, so we assign I%=1E-5 - line 36: small |I%|<1E-9 are zero, based on experiments, this threshold may vary on other machines' MachEps - line 39: if I%=0 then assign variables so that P, M, F and N are computed correctly with J/R=-1/N and K=R=S=1 and direct formula for N while avoiding div by zero when M=0 Add these four lines for amortization with period DEF-A after entering a TVM problem: Code: ' amortization for given period and next, accumulated int and prn are T and V, remaining balance R The results for the 12 TVM test examples look alright to me: Code: | # | Ref | N | I%YR | PV | PMT | FV | P/YR | End | Results for the C implementation of the same program with IEEE754 double precision, where I% is returned (to get I%YR multiply I% by 12 when P/YR=12 or multiply by N for tests 3 and 11). To compute the rate, the number of npmt function evaluations for root finding and retry counter G initialized to 2 are shown as evals/G. The retry counter G matters for TVM problems that dance around the root, such as n=40 pv=900 pmt=1000 fv=-1000 in BGN mode and n=360 pv=0 pmt=-1000 fv=1e+06 (not shown): Code: ======== 1 The C program and a script to run the tests: Code: #include <stdio.h> Code: ./tvm 40 ? 900 -40 -1000 1 - Rob "I count on old friends to remain rational" |
|||
06-11-2024, 10:32 PM
Post: #40
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
(06-11-2024 07:13 PM)robve Wrote: An improved version of the TVM program for SHARPs. Great work!! Very impressive to fit all this in these lines of code. Also a very interesting performance profile. Eyeballing it, very close to the HP-37, sometimes identical. I will put it into the results table and I'm curious to run the dimension reduction on it again and see where it pops up. I'd be curious of a video of it in action solving tvm problems if you ever find the time. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 12 Guest(s)