(11C) (15C) Very fast binary converter + fast modulo
03-14-2018, 10:07 PM (This post was last modified: 04-05-2018 03:51 PM by Michael Zinn.)
Post: #1
 Michael Zinn Member Posts: 56 Joined: Mar 2018
(11C) (15C) Very fast binary converter + fast modulo
This program can convert numbers to binary quickly by using a lookup table to convert 4 bits at once. Internally, it also uses a fast modulo calculation.

How to use the three programs:

A: Create a lookup table in registers 1 to 15. This takes about 3 minutes.
B: Convert a number to binary. If you created the table this will be fast. Without the table it will be 3x slower.
D: y mod x

The B program reads flag 0: If it is clear it will use a slow algorithm, if it is set it will use the fast one instead. Program A clears flag 0, creates the lookup table and then sets flag 0.

In my custom notation the code looks like this:

Code:
 ; create lookup table A:   flag0 = false    ; slow algorithm   15   i=   0:               ; for(i = 15, i > 0, i--)     i     int     gosub B        ; to binary     register[i]=     decrementSkipEqual       goto 0   flag0 = true   return ; toBinary with lookup table B:   0                ; the result   register[0]=     ; exponent   x >< y   1:               ; begin loop     if(x == 0)       goto 4     register[16]=  ; store input     if(flag0)       goto 2       ; fast algorithm     ;else       ; slow algorithm       2       gosub D      ; (mod 2)       register[0]       10^x       *       +       1       register[0] +=       pop       register[16]       2       /       int       goto 1     ; fast (fetch 4 bits using the lookup table)     2:       ; fetch 4 bits from input       16       gosub D      ; (mod 16)       if(x == 0)         goto 3       ;else         ; lookup binary         i=         pop         register[i]       3:       ; shift result bits to the left and add to result       register[0]       10^x       *       +       ; increment left shift amount for next iteration       4       register[0] +=       pop       ; input shr 4       register[16]       16       /       int     goto 1 ; end loop   4:   pop   return ; fast modulo D:   register[17]=   /   fractional   register[17]   *   return

However, for entering it into the calculator, the classical button notation might be easier:

Code:
 LBL A CF 0 1 5 STO I LBL 0 RCL I GSB B STO (i) DSE GTO 0 SF 0 RTN LBL B 0 STO 0 x><y LBL 1 x=0 GTO 4 STO . 6 F? 0 GTO 2 2 GSB D RCL 0 10^x * + 1 STO + 0 R\/ RCL . 6 2 / int GTO 1 LBL 2 1 6 GSB D x=0 GTO 3 STO I R\/ RCL (i) LBL 3 RCL 0 10^x * + 4 STO + 0 R\/ RCL . 6 1 6 / int GTO 1 LBL 4 R\/ RTN LBL D STO . 7 / FRAC RCL 17 * RTN

If you want to know how I wrote this you can read my blog post about it.
03-16-2018, 01:08 PM (This post was last modified: 03-16-2018 01:10 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (11C) Very fast binary converter + fast modulo
(03-14-2018 10:07 PM)michaelzinn Wrote:  This program can convert numbers to binary quickly by using a lookup table to convert 4 bits at once.

That's a nice idea which results in a significantly faster program.

(03-14-2018 10:07 PM)michaelzinn Wrote:  Internally, it also uses a fast modulo calculation.

The program uses this routine at LBL D for calculating x mod 2. This works well with the implemented method, calculating 2 · frac(x/2), but it's not a good idea for a general modulo function: try 25 mod 7 to get 3,999999997 instead of 4.

A better method for a mod b is  a – b · int(a/b):

Code:
LBL D X<>Y STO .7 X<>Y / LastX X<>Y INT x RCL .7 X<>Y – RTN

Of course this is slower than the frac method, but it yields better results.

If you want to stay with the original method: this can be done without using a data register:

Code:
LBL D / LastX X<>Y FRAC x RTN

Oh, I just notice this is your first post here. So, welcome to the forum. :-)

Dieter
03-16-2018, 03:47 PM
Post: #3
 Gene Moderator Posts: 1,034 Joined: Dec 2013
RE: (11C) Very fast binary converter + fast modulo
Yes, Michael... welcome to the forum. Please post more often.

Don't worry... Dieter can reprogram ANYONE's efforts and make them better. :-) He always does and I am very glad about it.

The key is to post ideas, thoughts, programs, and then we all can learn and sometimes help improve.

Welcome to the community here!
03-17-2018, 06:06 PM
Post: #4
 Michael Zinn Member Posts: 56 Joined: Mar 2018
RE: (11C) Very fast binary converter + fast modulo
Hi!

>The program uses this routine at LBL D for calculating x mod 2. This works well with the implemented method, calculating 2 · frac(x/2), but it's not a good idea for a general modulo function: try 25 mod 7 to get 3,999999997 instead of 4.

I actually tested that and it gave me 4, but I don't have a real calculator so I'm doing all this in the i11 Android app. Can anyone recommend a more accurate emulator?

> If you want to stay with the original method: this can be done without using a data register:

Heh, I ignored LastX and how stack lifting works because I didn't understand it quickly. I guess I should go back to reading the manual.

Also, how exactly are numbers stored in registers? The app I'm using might be inaccurate here, do real HP calculators use decimals with a floating point? Or is it binary?

> welcome to the [...]
Thank you both
03-17-2018, 09:17 PM (This post was last modified: 03-17-2018 09:20 PM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (11C) Very fast binary converter + fast modulo
(03-17-2018 06:06 PM)michaelzinn Wrote:  > try 25 mod 7 to get 3,999999997 instead of 4.
I actually tested that and it gave me 4, but I don't have a real calculator so I'm doing all this in the i11 Android app.

The basic problem is that it is impossible to exactly represent all fractions with a limited number of digits. 1/7 = 0,142857142857...., so multiplying this by 7 again with finite precision will never yield an integer result. Rounding helps here and there, but not in any case.

In the 25 mod 7 case this happens with 10-digit precision:

25/7 => 3,571428571
FRAC => 0,5714285710
x 7 => 3,9999999997

It gets even worse with 25000 mod 7:

25000/7 => 3571,428571
FRAC => 0,4285710000  // now only six decimals are left !
x 7 => 2,999997000

(03-17-2018 06:06 PM)michaelzinn Wrote:  Can anyone recommend a more accurate emulator?

Sorry, no idea. But you seem to use a simulator instead of an an emulator (which would reproduce the real thing exactly, running the original firmware). If, for instance, the calculations in your simulator are performend in standard 15-digit binary arithmetics, roundoff errors like the one above are covered since only 10 digits are visible in the display.

What do you get if you see the result "4" with the modulo routine and then subtract 4 from this? It is 0 or something different?
And what is the result if you calculate 10/3 (=3,333333333) and then double the result? Is it 6,666666666 or 6,666666667 ?

(03-17-2018 06:06 PM)michaelzinn Wrote:  Heh, I ignored LastX and how stack lifting works because I didn't understand it quickly. I guess I should go back to reading the manual.

From your blog post I understand that all this is new to you and something like ..."vintage computing", while most of the members here have grown up with all this, and RPN is nothing that one even has to think about – it feels natural and intuitive. ;-)

(03-17-2018 06:06 PM)michaelzinn Wrote:  Also, how exactly are numbers stored in registers? The app I'm using might be inaccurate here, do real HP calculators use decimals with a floating point? Or is it binary?

No, it's decimal arithmetics (BCD). The internal registers consist of 7 bytes = 56 bits with one nybble (4 bits) for each digit in mantissa (10) and exponent (2), and two more for their respective signs.

Since the mid-seventies most HPs use extended internal precision with 3 more guard digits for their calculations. This significantly improves the accuracy of the more complex functions since these are calculated with 13-digit internal precision before the result is rounded back to 10 digits and returned to the user.

Dieter
03-18-2018, 12:14 AM (This post was last modified: 03-19-2018 12:09 AM by Michael Zinn.)
Post: #6
 Michael Zinn Member Posts: 56 Joined: Mar 2018
RE: (11C) Very fast binary converter + fast modulo
Thank you, that is very helpful. I guess this means that multiplication/division by 2/5/10 etc. is totally safe .

The simulator seems to use binary, but it uses so much bits that I get about 15 decimal digits of precision (e.g. if I do 1/3 it looks like 0.333333333333333 followed by a bunch of random looking digits).

25000 mod 7 results in 3.

Yes, I see this as a fun vintage computing experience. I wanted to try something that was new to me and solving problems by programming the calculator is an interesting puzzle (It's very far removed from high level languages like Haskell or even Java).

04-05-2018, 03:25 PM (This post was last modified: 04-06-2018 08:32 PM by Michael Zinn.)
Post: #7
 Michael Zinn Member Posts: 56 Joined: Mar 2018
RE: (11C) Very fast binary converter + fast modulo
I'm currently running out of labels and registers so here is a slow, compact version that only uses register I and .6 and only one extra label (for 15C):

Code:
; toBinary(x) LBL 5:   FIX 0   0              ; put 0 on the stack as the result "variable"   STO I          ; initialize exponent to 0   x >< y   LBL 6               ; begin while loop     STO .6     2     GSB 2        ; (mod 2)     RCL I     10^x     *     +     1     STO + I     R v     RCL .6     2              ;     /              ; right shift     INT            ;     TEST 0 (x != 0)       GTO 6         ; end while loop   R v   RTN
 « Next Oldest | Next Newest »

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