This is an implementation of Crawl's suggestion (message #7 in this old thread). The convergence is fast, but because successive approximations oscillate as x approaches -1/e, the comparison test is made between yn+1 and yn-1 instead of yn+1 and yn. Perhaps using a flag would be a better solution than incrementing the register 03 and doing MOD 2 to save every other approximation into the register 04 as I did, but that's the only idea I have had. The running times on my HP-41CV are quite reasonable all through the valid ranges, but some optimization is possible, in case someone wants to try:
- using the stack to save one or two registers;
- avoiding numerical constants inside the loop to improve the speed;
- etc.
A WP 34S port should be also interesting, at it appears Wm takes too long on it and even on the emulator when x approaches -1/e, at least in my outdated version.
001*LBL "LWM"
002 STO M
003 CHS
004 LN
005 GTO 00
006*LBL "LWP"
007 STO 01
008 2
009 +
010 LN
011 ENTER^
012 LN
013 -
014*LBL 00
015 STO 02
016 STO 04
017 0
018 STO 03
019*LBL 01
020 1
021 ST+ 03
022 RCL 02
023 +
024 LASTX
025 X^2
026 RCL 01
027 LASTX
028 E^X
029 /
030 +
031 X<>Y
032 /
033 RCL 02
034 X<>Y
035 STO 02
036 RCL 03
037 2
038 MOD
039 X=0?
040 GTO 02
041 RDN
042 X<>Y
043 STO 04
044*LBL 02
045 RCL 02
046 RCL 04
047 X#Y?
048 GTO 01
049 RCL 02
050 RTN
051 .END.actual time
x LWP(x) LWP(x) ( s )9.999999e99 224.8431064 224.8431063 4.8
99.999999990 3.385630140 3.385630140 6.6
2.000000000 0.852605502 0.852605502 7.6
1.000000000 0.567143290 0.567143290 11.5
0.5 0.351733711 0.351733711 10.1
0.000000000 0.000000000 0.000000000 14.0
-0.367879441 -0.999969978 -0.999969471 22.6
-0.36787944 -0.999918898 -0.999920199 20.5
-0.3678794 -0.999526969 -0.999526967 20.4
-0.367 -0.932399183 -0.932399185 14.5
-0.36 -0.806084317 -0.806084316 12.6
-0.32 -0.560489483 -0.560489483 12.7
-0.3 -0.489402227 -0.489402227 10.5
-0.2 -0.259171102 -0.259171102 9.8
-0.000100000 -0.000100010 -0.000100010 9.2
-1e-10 -1e-10 -1e-10 9.1
-1e-20 -2e-20 -2e-20 9.1
-9e-90 -9e-90 -9e-90 10.7actual time
x LWM(x) LWM(x) ( s )-0.36787944 -1.000086314 -1.000079806 15.8
-0.3678794 -1.000047374 -1.000047318 15.8
-0.367 -1.070791895 -1.070791887 14.0
-0.36 -1.222770133 -1.222770134 10.4
-0.32 -1.624849446 -1.624849446 7.7
-0.3 -1.781337024 -1.781337023 5.8
-0.2 -2.542641358 -2.542641358 5.7
-0.000000001 -23.89701959 -23.89701959 9.4
-1e-20 -49.96298427 -49.96298428 9.4
-2e-60 -142.4207440 -142.4207440 11.5
-9e-90 -210.3843700 -210.3843700 11.3