This is somewhat limited by the definition of DSE unfortunately.
Here is the sum and product code as it currently exists in the firmware image. It is just a plain old keystroke program. Feel free to submit a patch :-)
The stack will always be four levels deep when this code is run and registers 0-4 and flags 0-14 are freely available.
Then again, I could look into adding DSL and ISE as instructions.
- Pauli
/**************************************************************************/
/* Sigma and products
* Register use:
* 0 I
* 1 product/sum
* 2 carry for sum
* 3 saved I
*/
LBL 99 /* Entry: SUMMATION */
XEQ entry
SPEC?
JMP sum_product_okay
STO 00
STO 03 /* Save for LastX*/
IP /* First function call is separate*/
FILL /* to avoid Kahan summing from zero*/
XEQUSR /* six extra instructions save nine*/
SPEC? /* from executing*/
JMP sum_product_nan
STO 01
iC 0
STO 02
JMP sum_entrysum_loop:: RCL 00
IP
FILL
XEQUSR
SPEC?
JMP sum_product_nan
RCL- 02
ENTER[^]
RCL+ 01
ENTER[^]
RCL- 01
RCL- Z
x[<->]y
[cmplx]STO 01sum_entry:: DSE 00
JMP sum_loop
JMP sum_product_okay
LBL 98 /* Entry: PRODUCT */
XEQ entry
SPEC?
JMP sum_product_okay
STO 00
STO 03
IP /* First function call is separate*/
FILL /* to avoid a multiply*/
XEQUSR
SPEC?
JMP sum_product_nan
STO 01
JMP product_entryproduct_loop:: RCL 00
IP
FILL
XEQUSR
SPEC?
JMP sum_product_nan
STO[times] 01
product_entry:: DSE 00
JMP product_loopsum_product_okay:: RCL 03
STO L
iC 0
FILL
RCL 01
JMP exitsum_product_nan:: RCL 03
sum_product_error:: STO L
iC 0
FILL
# NaN
JMP exit
DSL and ISE are now present :-)
- Pauli
... and documented in the manual :-)
Walter
Pauli, what is the reasoning behind the complicated summation. It must have to do with increasing accuracy but how does it work?
If you want to make the code shorter, the two loops may easily be merged and the operation selected by a flag inside the loop.
The summation is indeed an attempt to improve precision. The algorithm used is known as the Kahan sum.
Essentially, it maintains the sum and some of the digits lost off the end in two registers. The lost digits act as a correction term as each new item is added in.
Yes, the two loops could be merged. I didn't investigate this option since neither is very large.
- Pauli
We will certainly be fighting for a handful of bytes again...