Post Reply 
Dismal Arithmetic & 3 Progs for 49G & Others
04-21-2015, 04:18 PM (This post was last modified: 09-08-2022 06:14 AM by Gerald H.)
Post: #1
Dismal Arithmetic & 3 Progs for 49G & Others
Dismal arithmetic was invented to help less proficient arithmeticians to achieve easier results. There are two operations, addition & multiplication.

Addition is no-carry & the sum of two digits is the larger of the two.

eg 123456789 + 987654321 = 987656789

Multiplication is no carry & the product of two digits is the lesser of the two.

eg 12345678901 * 7 = 12345677701

The first few dismal squares are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 111, 112, 113, 114, 115, 116, 117, 118, 119, 200, 211....

For more see:

http://oeis.org/A087019

For more info & better explanations than I can give, try these three links:

https://proofwiki.org/wiki/Definition:Dismal_Addition

https://oeis.org/wiki/Dismal_arithmetic

http://arxiv.org/pdf/1107.1130.pdf

Two programmes for the HP 49G; DPLUS accomplishes dismal addition of two positive integers & DMULT the dismal multiplication of two positive integers.

DPLUS

Code:
::
  CK2&Dispatch
  # FFFF
  ::
    NULL$
    3UNROLL
    FPTR2 ^Z>S
    xSREV
    SWAP
    FPTR2 ^Z>S
    xSREV
    OVERLEN$
    OVERLEN$
    #MIN
    #1+_ONE_DO
    DUP
    CDR$
    SWAP
    CAR$
    ROTDUP
    CDR$
    3UNROLL
    CAR$
    2DUP#<
    ?SWAPDROP
    4ROLLSWAP
    >H$
    3UNROLL
    LOOP
    &$
    xSREV
    SWAP&$
    FPTR2 ^S>Z
  ;
;

DMULT

Code:
::
  CK2&Dispatch
  # FFFF
  ::
    DUP
    PTR 2F3A3
    COERCE
    DUP#0=csedrp
    FPTR2 ^2DROPZ0
    Z0_
    4UNROLL
    #1+_ONE_DO
    Z10_
    FPTR2 ^ZDIVext
    3PICKSWAP
    NULL$
    3UNROLL
    FPTR2 ^Z>S
    CAR$
    SWAP
    FPTR2 ^Z>S
    SWAP
    OVERLEN$
    #1+_ONE_DO
    DUPROT
    DUP
    CAR$
    SWAP
    CDR$
    4UNROLL
    2DUP#>
    ?SWAPDROP
    4ROLLSWAP
    >T$
    3UNROLL
    LOOP
    2DROP
    FPTR2 ^S>Z
    4ROLL
    ID DPLUS
    SWAPROT
    Z10_
    FPTR2 ^RMULText
    SWAPLOOP
    2DROP
  ;
;

I eagerly await the appearance of some dismal arithmetic programmes on the Prime.
Find all posts by this user
Quote this message in a reply
04-21-2015, 09:19 PM
Post: #2
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-21-2015 04:18 PM)Gerald H Wrote:  I eagerly await the appearance of some dismal arithmetic programmes on the Prime.

Here is my version of DPLUS and DMULT for the Prime:

Code:
EXPORT DPLUS(a,b)
BEGIN
LOCAL dlg,la,lb;
  la:=ASC(STRING(a)); lb:=ASC(STRING(b));
  dlg:=SIZE(la)-SIZE(lb);
  IF dlg<0 THEN la:=CONCAT(MAKELIST(0,X,1,−dlg),la); END;
  IF dlg>0 THEN lb:=CONCAT(MAKELIST(0,X,1,dlg),lb); END;
  EXPR(CHAR(MAX(la,lb)));
END;

EXPORT DMULT(a,b)
BEGIN
LOCAL la,lb,i,p;
  la:=ASC(STRING(a)); lb:=ASC(STRING(b));
  FOR i FROM 1 TO SIZE(lb) DO
    p:=DPLUS(p*10,EXPR(CHAR(MIN(la,lb(i)))));
  END;
END;
Find all posts by this user
Quote this message in a reply
04-22-2015, 01:09 AM
Post: #3
RE: Dismal Arithmetic with Two Programmes for the HP 49G
Rather interesting concept. Does this have any practical applications?
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2015, 01:18 AM
Post: #4
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-22-2015 01:09 AM)Dave Britten Wrote:  Rather interesting concept. Does this have any practical applications?

I'm not sure about base-10 dismal arithmetic, but the base-2 equivalent is quite commonly used. The base-2 dismal addition and multiplication operators are the logical OR and AND operators, respectively.
Find all posts by this user
Quote this message in a reply
04-22-2015, 06:55 AM
Post: #5
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-22-2015 01:18 AM)brouhaha Wrote:  I'm not sure about base-10 dismal arithmetic, but the base-2 equivalent is quite commonly used. The base-2 dismal addition and multiplication operators are the logical OR and AND operators, respectively.
With different results of course.
More on this here.
Find all posts by this user
Quote this message in a reply
04-22-2015, 07:04 AM
Post: #6
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-21-2015 09:19 PM)Didier Lachieze Wrote:  
(04-21-2015 04:18 PM)Gerald H Wrote:  I eagerly await the appearance of some dismal arithmetic programmes on the Prime.

Here is my version of DPLUS and DMULT for the Prime:

Code:
EXPORT DPLUS(a,b)
BEGIN
LOCAL dlg,la,lb;
  la:=ASC(STRING(a)); lb:=ASC(STRING(b));
  dlg:=SIZE(la)-SIZE(lb);
  IF dlg<0 THEN la:=CONCAT(MAKELIST(0,X,1,−dlg),la); END;
  IF dlg>0 THEN lb:=CONCAT(MAKELIST(0,X,1,dlg),lb); END;
  EXPR(CHAR(MAX(la,lb)));
END;

EXPORT DMULT(a,b)
BEGIN
LOCAL la,lb,i,p;
  la:=ASC(STRING(a)); lb:=ASC(STRING(b));
  FOR i FROM 1 TO SIZE(lb) DO
    p:=DPLUS(p*10,EXPR(CHAR(MIN(la,lb(i)))));
  END;
END;

The programmes are nicely short & may work - I started to type them into the Prime & gave up due to the obscurity of the keyboard.

I guess I'll have to wait until I'm issued with new eyes as I expect that'll happen before a better keyboard is produced.
Find all posts by this user
Quote this message in a reply
04-22-2015, 07:16 AM
Post: #7
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-22-2015 07:04 AM)Gerald H Wrote:  The programmes are nicely short & may work - I started to type them into the Prime & gave up due to the obscurity of the keyboard.

I guess I'll have to wait until I'm issued with new eyes as I expect that'll happen before a better keyboard is produced.

I agree with the keyboard contrast issue and I have improved it with some stickers on my Prime.

Anyway you don't have to type each single character on the Prime keyboard, all functions are available in menus or catalogs. Then you have to memorize a few characters, mainly ":", "=" and ";".

Or you can simply use the connectivity kit, and copy paste the programs for use in the Emulator or your real Prime.
Find all posts by this user
Quote this message in a reply
04-22-2015, 07:45 AM
Post: #8
RE: Dismal Arithmetic with Two Programmes for the HP 49G
Here is a slightly improved version of DPLUS using this trick for string padding:

Code:
EXPORT DPLUS(a,b)
BEGIN
LOCAL la,lb;
  la:=ASC(REPLACE("",13-DIM(STRING(a)),STRING(a))); 
  lb:=ASC(REPLACE("",13-DIM(STRING(b)),STRING(b))); 
  EXPR(CHAR(MAX(la,lb)));
END;

EXPORT DMULT(a,b)
BEGIN
LOCAL la,lb,i,p;
  la:=ASC(STRING(a)); lb:=ASC(STRING(b));
  FOR i FROM 1 TO SIZE(lb) DO
    p:=DPLUS(p*10,EXPR(CHAR(MIN(la,lb(i)))));
  END;
END;
Find all posts by this user
Quote this message in a reply
04-22-2015, 08:54 AM
Post: #9
RE: Dismal Arithmetic with Two Programmes for the HP 49G
Dear Didier,

Thank you for your efforts to help me.

I have two problems in implementing your suggestion, one is a question of principle, the second practical.

1 A calculator should be a portable independent unit. All of the programmes I have written for 48G, 42S, 49G & 50g have been written on the calculator, occasionally in the middle of nowhere, ie no mains electricity. Having to download from an object the size of a lap-top or PC is simply not practicable.

2 I have downloaded your progs & stored them in pure text files on my home computer but they aren't accepted by the calculator.

Again, thanks for the assistance.
Find all posts by this user
Quote this message in a reply
04-22-2015, 09:56 AM
Post: #10
RE: Dismal Arithmetic with Two Programmes for the HP 49G
(04-22-2015 08:54 AM)Gerald H Wrote:  I have two problems in implementing your suggestion, one is a question of principle, the second practical.

1 A calculator should be a portable independent unit. All of the programmes I have written for 48G, 42S, 49G & 50g have been written on the calculator, occasionally in the middle of nowhere, ie no mains electricity. Having to download from an object the size of a lap-top or PC is simply not practicable.

I agree, and I can tell you that yesterday evening I’ve written these programs directly on my Prime. The only point is that with calculators as complex as the 48/50 or Prime you need some practice to be used to the keyboard and where to find the different functions. It can be easier to write programs on a computer when possible. Of course in case of the Prime the keyboard contrast makes it a bit more difficult for aging eyes … (and mine are also on the aging side now).

(04-22-2015 08:54 AM)Gerald H Wrote:  2 I have downloaded your progs & stored them in pure text files on my home computer but they aren't accepted by the calculator.

Well, I just copy/paste from the forum to the connectivity kit after creating a new program on the Prime from the connectivity kit (right click on “Programs” and select “New”) and it works fine. Anyway I’ve attached the .hpprgm file that you should be able to directly drag and drop in the connectivity kit, then you just need to open it on your Prime and Check it before using it.

.zip  Dismal.zip (Size: 417 bytes / Downloads: 5)
Find all posts by this user
Quote this message in a reply
04-22-2015, 10:07 AM
Post: #11
RE: Dismal Arithmetic with Two Programmes for the HP 49G
Downloaded, unzipped & tested - results correct so far.

Thank you.
Find all posts by this user
Quote this message in a reply
05-06-2015, 05:34 PM
Post: #12
RE: Dismal Arithmetic with Two Programmes for the HP 49G & the Prime
Here's a straight-forward UserRPL version I threw together on my 48SX. It's only intended to operate on non-negative integers; passing anything else has undefined results.

Code:
NUMLEN:
<<
  IF DUP 0 !=
  THEN ABS LOG 1 + IP
  END
>>

DIGIT:
<< 1 - ALOG / IP 10 MOD >>

DADD:
<< 0 0 -> A B C M
  << A NUMLEN B NUMLEN MAX 'M' STO
    1 M FOR I
      A I DIGIT B I DIGIT MAX I 1 - ALOG * 'C' STO+
    NEXT
    C
  >>
>>

DMULT:
<< 0 0 0 0 -> A B D M P S
  << B NUMLEN 'M' STO
    A NUMLEN
    IF DUP 0 !=
    THEN
      1 FOR I
        0 'P' STO A I DIGIT 'D' STO
        1 M FOR J
          B J DIGIT D MIN J 1 - ALOG * 'P' STO+
        NEXT
        S 10 * P DADD 'S' STO
      -1 STEP
      S
    END
  >>
>>

Usage is pretty straight-forward. Enter your two operands on the stack, and run DADD or DMULT. I might take a stab at doing a 32S version.
Visit this user's website Find all posts by this user
Quote this message in a reply
05-07-2015, 01:48 PM
Post: #13
RE: Dismal Arithmetic with Two Programmes for the HP 49G & the Prime & the HP 48S
And here's a 32S version for the terminally bored. It probably works on a 32Sii or 33S/35S, though the sizes, checksums, and speed may differ.

Multiplying 435,821 by 2,949,229 takes about 23 seconds my 32S (rotten nested loops...) and yields 244,585,835,821.

Code:
Total bytes: 166.5
Variables: A, B, C, D, G, H, I, J, K, L, M
Labels: A, B, D, M, N, O, P

Enter two arguments, then XEQ A to "add", or XEQ M to "multiply".

N01    LBL N    013.5 82EF
N02    x=0?
N03    RTN
N04    ABS
N05    LOG
N06    1
N07    +
N08    IP
N09    RTN

D01    LBL D    018.0 B250
D02    1
D03    -
D04    10^x
D05    /
D06    IP
D07    10
D08    /
D09    FP
D10    10
D11    *
D12    RTN

A01    LBL A    024.0 D23B
A02    STO B
A03    XEQ N
A04    x<>y
A05    STO A
A06    XEQ N
A07    x<y?
A08    x<>y
A09    3
A10    10^x
A11    /
A12    1
A13    +
A14    STO D
A15    0
A16    STO C

B01    LBL B    033.0 5FB3
B02    RCL A
B03    RCL D
B04    IP
B05    XEQ D
B06    RCL B
B07    RCL D
B08    IP
B09    XEQ D
B10    x<y?
B11    x<>y
B12    RCL D
B13    IP
B14    1
B15    -
B16    10^x
B17    *
B18    STO+ C
B19    ISG D
B20    GTO B
B21    RCL C
B22    RTN

M01    LBL M    019.5 C0E6
M02    STO H
M03    XEQ N
M04    STO J
M05    x<>y
M06    STO G
M07    XEQ N
M08    3
M09    10^x
M10    /
M11    STO K
M12    0
M13    STO I

O01    LBL O    019.5 525E
O02    0
O03    STO L
O04    RCL H
O05    RCL J
O06    IP
O07    XEQ D
O08    STO M
O09    RCL K
O10    FP
O11    1
O12    +
O13    STO K

P01    LBL P    039.0 18BA
P02    RCL M
P03    RCL G
P04    RCL K
P05    IP
P06    XEQ D
P07    x>y?
P08    x<>y
P09    RCL K
P10    IP
P11    1
P12    -
P13    10^x
P14    *
P15    STO+ L
P16    ISG K
P17    GTO P
P18    10
P19    RCL* I
P20    RCL L
P21    XEQ A
P22    STO I
P23    DSE J
P24    GTO O
P25    RCL I
P26    RTN


Labels
A - Add
B - Adding loop

M - Multiply
O - Multiply outer loop
P - Multiply inner loop

N - Number length routine
D - Get digit routine

Variables

A - Addend 1
B - Addend 2
C - Sum
D - Add loop counter

G - Factor 1
H - Factor 2
I - Sum of products
J - Outer loop (factor 2) counter
K - Inner loop (factor 1) counter
L - Product
M - Outer loop (factor 2) current digit
Visit this user's website Find all posts by this user
Quote this message in a reply
05-07-2015, 03:25 PM
Post: #14
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
(05-07-2015 01:48 PM)Dave Britten Wrote:  Multiplying 435,821 by 2,949,229 takes about 23 seconds my 32S (rotten nested loops...) and yields 244,585,835,821.

Which I agree is the right answer - nice to have a check & nice programmes. My 35S is lying useless, so maybe it has a dismal future.
Find all posts by this user
Quote this message in a reply
05-07-2015, 03:57 PM (This post was last modified: 05-07-2015 04:59 PM by Gerald H.)
Post: #15
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
My 32S II using Dave's programmes unaltered takes 28 sec for 435,821 dmult 2,949,229. Answer again correct.
Find all posts by this user
Quote this message in a reply
05-07-2015, 04:59 PM (This post was last modified: 05-07-2015 05:00 PM by Gerald H.)
Post: #16
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
My 35S using Dave's programmes unaltered takes 56 sec for 435,821 dmult 2,949,229. Answer again correct.
Find all posts by this user
Quote this message in a reply
05-07-2015, 05:36 PM
Post: #17
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
Yeah, that perpetual slowdown of the family is one of the main reasons I stick with the original 32S. The other reasons being that my 32Sii has a janky keyboard, and the 35S is full of bugs.
Visit this user's website Find all posts by this user
Quote this message in a reply
05-07-2015, 06:01 PM
Post: #18
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
My 32S, SNr 3246S.... keyboard in order - which I can't say of the 35S, SNr CNA 021..... where some of the keys are erratically unresponsive.
Find all posts by this user
Quote this message in a reply
05-08-2015, 01:35 AM
Post: #19
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
For those of you who have already died of boredom, here's a TI-95 version. It's not terribly fast. Even with all the control transfer statements ASMed, it took 42 seconds to compute 435,821 * 2,949,229.

Run the program to get the main menu, then separate your two arguments with x~t and press F1 or F2 to add or multiply. (As if anybody here besides me has a TI-95 and cares enough to try this.)

Code:
280 Bytes

DFN CLR
DFN F1:ADD@AD
DFN F2:MLT@ML
RTN

LBL NL
IF= 014
RTN
ABS
LOG
+
1
=
INT
RTN

LBL DG
-
1
=
INV
LOG
x~t
/
x~t
=
INT
/
10
=
FRC
*
10
=
RTN

LBL AD
STO 001
SBL NL
STO 002
x~t
STO 000
SBL NL
IF> 002
STO 002
1
STO 003
0
STO 005
STO 014

LBL A1
RCL 000
x~t
RCL 003
SBL DG
STO 004
RCL 001
x~t
RCL 003
SBL DG
IF< 004
RCL 004
*
(
RCL 003
-
1
)
INV
LOG
=
ST+ 005
INC 003
RCL 003
INV
IF> 002
GTL A1
RCL 005
RTN

LBL ML
STO 007
SBL NL
STO 010
x~t
STO 006
SBL NL
STO 008
0
STO 013
STO 014

LBL M1
0
STO 012
RCL 007
x~t
RCL 010
SBL DG
STO 011
1
STO 009

LBL M2
RCL 006
x~t
RCL 009
SBL DG
IF> 011
RCL 011
*
(
RCL 009
-
1
)
INV
LOG
=
ST+ 012
INC 009
RCL 009
INV
IF> 008
GTO M2
RCL 013
*
10
=
x~t
RCL 012
SBL AD
STO 013
DSZ 010
GTL M1
RCL 013
RTN


Labels
NL - Number length
DG - Get digit

AD - Add
A1 - Add loop

ML - Multiply
M1 - Multiply outer loop
M2 - Multiply inner loop


Variables
000 - Addend 1
001 - Addend 2
002 - Addend max length (loop end)
003 - Add loop counter
004 - Addition scratch register
005 - Sum

006 - Factor 1
007 - Factor 2
008 - Factor 1 length (inner loop end)
009 - Inner loop counter, incrementing (factor 1)
010 - Outer loop counter, decrementing (factor 2)
011 - Outer loop current digit (factor 2)
012 - Product
013 - Sum of products

014 - Zero (constant for comparison tests)

Some notes:

If you ASM and then subsequently INV ASM the program, the GTL M1 instruction doesn't get converted back from the step number. No idea why.

The comparison tests are very flexible, yet limited at the same time. You can compare the display register (x) with any storage register and skip the next instruction if false, but there's no way to compare x with 0 without storing 0 somewhere, and you can't directly test x against t unless you unprotect the system and access it via system register number 2079.

The lack of ISG is surprisingly annoying.

I may attempt doing this on the TI-66 if I hit my head and forget how awful the comparison tests are on that. (You only get x=t, x!=t, x>=t, and x<t, and you're only able to have an implied GTO after it, so doing a conditional RTN requires a label followed by a RTN floating around in your program somewhere.)
Visit this user's website Find all posts by this user
Quote this message in a reply
05-08-2015, 02:53 PM
Post: #20
RE: Dismal Arithmetic with Two Programmes 49G, the Prime, 48S & 32S
(At least) one more: Casio fx-8500G. 435,821 * 2,949,229 takes 16 seconds, but seems to overflow the precision of the machine. I get 244,585,835,800 instead of 244,585,835,821.

Code:
Prog 3 - Number Length
Ans=0=>Goto 9
Int log Ans+1
Lbl 9

Prog 4 - Get Digit
Frac (Int (N/10^(D-1))/10)*10

Prog 5 - Add
G=1=>Goto 0
"X"?->A
"Y"?->B
Lbl 0
0->G
A
Prog 3
Ans->C
B
Prog 3
Ans>C=>Ans->C
1->I
0->S
Lbl 1
A->N
I->D
Prog 4
Ans->T
B->N
Prog 4
Ans>T=>Ans->
T*10^(I-1)+S->S
I+I->I
I<=C=>Goto 1
S

Prog 6 - Multiply
"X"?->X
"Y"?->Y
X
Prog 3
Ans->Z
Y
Prog 3
Ans->J
0->F
Lbl 1
Y->N
J->D
Prog 4
Ans->M
1->K
0->P
Lbl 2
M->U
X->N
K->D
Prog 4
Ans<U=>Ans->U
U*10^(K-1)+P->P
1+K->K
K<=Z=>Goto 2
P->A
F*10->B
1->G
Prog 5
Ans->F
Dsz J:Goto 1
F


Variables
G - "Don't Prompt" flag (interactive vs. called by multiplication program)
A - Addend 1
B - Addend 2
C - Add loop limit (addend max length)
I - Add loop counter
S - Sum
T - Addition scratch register (digit comparison)

X - Factor 1
Y - Factor 2
Z - Factor 1 length
J - Factor 2 length/outer loop counter
K - Inner loop counter (factor 1)
P - Product
F - Sum of products
M - Factor 2 current digit
U - Multiply scratch register (digit comparison)

N - Input number
D - Input digit
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)