A tiny synthetic HP41 challenge
12-07-2016, 07:17 PM (This post was last modified: 12-07-2016 07:21 PM by Dieter.)
Post: #1
 Dieter Senior Member Posts: 2,398 Joined: Dec 2013
A tiny synthetic HP41 challenge
I'm not an expert if it comes to synthetic programming on the HP41, so probably someone else here may have a better idea than me for the following little challenge.

The task: truncate a number after six or seven significant digits and normalize it to the domain 0 <= X < 1.

Some examples:
3,141592654 => 0,314159 or 0,3141592
–1,234567809 E–32 => 0,123456 or 0,1234567
9,999999999 E+99 => 0,999999 or 0,9999999
0,123456 => 0,123456

Here's my solution:

Code:
CLA ABS STO M ASTO M ASTO M ASTO M >"\x00\x09\x99" RCL M CLA END

The append command adds three bytes (decimal 0, 9, 153 or hex 00, 09, 99) to clear the last three digits and set the exponent to –1.

I am sure you can do better. So what's your idea here?

Dieter
12-08-2016, 06:25 PM
Post: #2
 hth Member Posts: 290 Joined: Mar 2014
RE: A tiny synthetic HP41 challenge
I am not good at synthetics at all. Your solution looks clever, but I think it will not handle the case when X is 0 properly. I also think you can just remove the last CLA, there is no need to clean up.

Håkan
12-08-2016, 06:46 PM
Post: #3
 Dieter Senior Member Posts: 2,398 Joined: Dec 2013
RE: A tiny synthetic HP41 challenge
(12-08-2016 06:25 PM)hth Wrote:  I am not good at synthetics at all. Your solution looks clever, but I think it will not handle the case when X is 0 properly.

Thank you for your reply. Of course the code works for x=0. A look at the M-register shows what it does. Assume x=pi.

Code:
CLA        00 00 00 00 00 00 00 STO M      03 14 15 92 65 40 00 ASTO M     10 03 14 15 92 65 40 ASTO M     10 10 03 14 15 92 65 ASTO M     10 10 10 03 14 15 92 >"000999"  10 10 10 03 14 15 92 00 09 99        ...-- N ---> <--- register M ---> RCL M => 3,141592000 E-1        = 0,3141592

I hoped for an elegant method of shifting the value by a nybble (half a byte) so that also results with 4, 6 or 8 digits become possible. Or something more elegant than three ASTO commands. ;-)

(12-08-2016 06:25 PM)hth Wrote:  I also think you can just remove the last CLA, there is no need to clean up.

I should have known this 40 years ago: "No, Mom, my room is OK this way. Håkan said there is no need to clean up". ;-)

Dieter
12-08-2016, 08:34 PM
Post: #4
 hth Member Posts: 290 Joined: Mar 2014
RE: A tiny synthetic HP41 challenge
(12-08-2016 06:46 PM)Dieter Wrote:  Thank you for your reply. Of course the code works for x=0. A look at the M-register shows what it does. Assume x=pi.

Code:
CLA        00 00 00 00 00 00 00 STO M      03 14 15 92 65 40 00 ASTO M     10 03 14 15 92 65 40 ASTO M     10 10 03 14 15 92 65 ASTO M     10 10 10 03 14 15 92 >"000999"  10 10 10 03 14 15 92 00 09 99        ...-- N ---> <--- register M ---> RCL M => 3,141592000 E-1        = 0,3141592

If you have 0 as input and you append the 999 exponent at the end, then RCL M will put a non-normalized number in X?

(12-08-2016 06:46 PM)Dieter Wrote:
(12-08-2016 06:25 PM)hth Wrote:  I also think you can just remove the last CLA, there is no need to clean up.

I should have known this 40 years ago: "No, Mom, my room is OK this way. Håkan said there is no need to clean up". ;-)

Dieter

LOL!

Håkan
12-08-2016, 09:17 PM (This post was last modified: 12-08-2016 09:18 PM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,398 Joined: Dec 2013
RE: A tiny synthetic HP41 challenge
(12-08-2016 08:34 PM)hth Wrote:  If you have 0 as input and you append the 999 exponent at the end, then RCL M will put a non-normalized number in X?

Ah, OK, that's of course correct. But the next operation or a STO/RCL should normalize it.
You can also do it within the routine. For instance by adding zero, like it's done in "MT" of the PPC ROM:

Code:
CLA ABS STO M ASTO M ASTO M ASTO M >"\x00\x09\x99" CLX ST+ M X<> M CLA END

Still including a final CLA for cleanup. ;-)

Dieter
 « Next Oldest | Next Newest »

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