(11C) (15C) Very fast binary converter + fast modulo

03142018, 10:07 PM
(This post was last modified: 04052018 03:51 PM by Michael Zinn.)
Post: #1




(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:
However, for entering it into the calculator, the classical button notation might be easier: Code:
If you want to know how I wrote this you can read my blog post about it. 

03162018, 01:08 PM
(This post was last modified: 03162018 01:10 PM by Dieter.)
Post: #2




RE: (11C) Very fast binary converter + fast modulo
(03142018 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. (03142018 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 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 Oh, I just notice this is your first post here. So, welcome to the forum. :) Dieter 

03162018, 03:47 PM
Post: #3




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! 

03172018, 06:06 PM
Post: #4




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 

03172018, 09:17 PM
(This post was last modified: 03172018 09:20 PM by Dieter.)
Post: #5




RE: (11C) Very fast binary converter + fast modulo
(03172018 06:06 PM)michaelzinn Wrote: > try 25 mod 7 to get 3,999999997 instead of 4. 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 10digit 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 (03172018 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 15digit 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 ? (03172018 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. ;) (03172018 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 midseventies 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 13digit internal precision before the result is rounded back to 10 digits and returned to the user. Dieter 

03182018, 12:14 AM
(This post was last modified: 03192018 12:09 AM by Michael Zinn.)
Post: #6




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). Thank you again for your very helpful responses. 

04052018, 03:25 PM
(This post was last modified: 04062018 08:32 PM by Michael Zinn.)
Post: #7




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) 

« Next Oldest  Next Newest »

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