[42S] MANT Challenge
01-22-2014, 09:40 AM
Post: #1
 Werner Senior Member Posts: 695 Joined: Dec 2013
[42S] MANT Challenge
On the 41, you can use Synthetic Programming to come up with a short and efficient routine that returns the mantissa.
Not so on the 42S. Is anyone willing to give it a try?
A few borderline cases that may foil your first attempts (on a real 42S, not Free42):

0
1.00000000001e-01
-9.99999999999
9.99999999999e499

Good luck,
Werner
01-22-2014, 12:23 PM
Post: #2
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 09:40 AM)Werner Wrote:  On the 41, you can use Synthetic Programming to come up with a short and efficient routine that returns the mantissa.
Not so on the 42S. Is anyone willing to give it a try?
A few borderline cases that may foil your first attempts (on a real 42S, not Free42):

0
1.00000000001e-01
-9.99999999999
9.99999999999e499

Good luck,
Werner

First attempt. Trouble with your last example. This should be quite easy on the HP-15C: 9 steps on my first attempt.
Code:
 00 { 20-Byte Prgm } 01>LBL "MANT" 02 X=0? 03 RTN 04 10 05 × 06 ABS 07 ENTER 08 LOG 09 IP 10 10^X 11 ÷ 12 .END.

Regards,

Gerson.
01-22-2014, 12:38 PM
Post: #3
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
Fails for 2e-02 and all 0<x<0.1 that are not 10^-n
Fails for 9.99999999999*10^n
The reason is that LOG(9.99999999999) = 1 exactly, on a real 42S
Werner
01-22-2014, 01:46 PM (This post was last modified: 01-22-2014 02:18 PM by Dieter.)
Post: #4
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 12:38 PM)Werner Wrote:  Fails for 2e-02 and all 0<x<0.1 that are not 10^-n
Fails for 9.99999999999*10^n
The reason is that LOG(9.99999999999) = 1 exactly, on a real 42S
Werner

Simple solution: forget mathematics, use Alpha.

Code:
01 CLA 02 SCI 11 03 ARCL ST X 04 -2 05 AROT 06 ATOX 07 ATOX 08 ANUM

A never owned a 42s, so maybe there's a more elegant way of deleting the last two characters. This will also work on the 41-series if SCI 11 is replaced by SCI 9. Since the display mode is changed, a final command that resets it may be added.

EDIT: Since always the last two characters are deleted, this only works for exponents up to ±99. The code can be adjusted accordingly, while keeping the original idea of using the internal formatting routine in Alpha mode:

Code:
01 CLA 02 SCI 11 03 ARCL ST X 04 ASTO ST X 05 ASHF 06 ASTO ST Y 07 ASHF 08 ATOX 09 ATOX 09 CLA 10 ARCL ST Z 11 ARCL ST T 12 X<>Y 13 XTOA 12 X<>Y 13 XTOA 14 ANUM

This should work for all possible cases. In both routines the sign of X is preserved in the resulting mantissa.

The basic idea is simple: Have X formatted in SCI mode and take the leftmost 14 characters. For X≥0 this may include a trailing "E" which is ignored when the string finally is converted back to a number.

Dieter
01-22-2014, 02:17 PM (This post was last modified: 01-22-2014 02:18 PM by Werner.)
Post: #5
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
MANT should return the unsigned mantissa, as in the 48.
Exponents are 1, 2 or 3 digits, and possibly negative of course.
Probably easier to remove the decimal point and use two ASTO's to X and L to get the 12 digits, then rebuild the integer in alpha and do ANUM.
Would still get quite long, I think.
Werner
01-22-2014, 02:24 PM
Post: #6
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 02:17 PM)Werner Wrote:  MANT should return the unsigned mantissa, as in the 48.

Then simply add an ABS at the beginning.

(01-22-2014 02:17 PM)Werner Wrote:  Exponents are 1, 2 or 3 digits, and possibly negative of course.
Probably easier to remove the decimal point and use two ASTO's to X and L to get the 12 digits, then rebuild the integer in alpha and do ANUM.
Would still get quite long, I think.
Werner

Note quite that long. The original routine already worked for all exponents within ±99, and X may be zero, negative or positive. The additional routine I posted in the meantime works for any exponent.

Dieter
01-22-2014, 02:34 PM (This post was last modified: 01-22-2014 02:46 PM by Werner.)
Post: #7
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
That would indeed work, if there were such a thing as ANUM on the 42S...
Back to square one...
Werner
01-22-2014, 06:50 PM
Post: #8
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 02:34 PM)Werner Wrote:  That would indeed work, if there were such a thing as ANUM on the 42S...
What? No ANUM on the 42s? I thought it featured the complete X-Functions command set (without the X-memory related ones, of course). The lack of this very powerful command really is a weak point.

Otherwise I could provide an even shorter version and one with a different approach. It works nicely on a 41 -- but without ANUM... #-\

But at least this routine could be used for display purposes. 8-)

Code:
01 ABS 02 CLA 03 SCI 11 04 ARCL ST X 05 ATOX 06 ASTO ST Y 07 ASHF 08 ASTO ST Z 09 " " 10 XTOA 11 ARCL ST Y 12 ARCL ST Z 13 LASTX 14 AVIEW

;-)

Dieter
01-22-2014, 07:53 PM (This post was last modified: 01-22-2014 08:01 PM by Dieter.)
Post: #9
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 09:40 AM)Werner Wrote:  A few borderline cases that may foil your first attempts (on a real 42S, not Free42):

0
1.00000000001e-01
-9.99999999999

The problem are values with a mantissa > 9,99999999988 or even > 9,99999998844 (near the end of the working range). Here the log10 will be rounded up to the next higher integer. So the idea is to divide by the next lower power of ten (which also handles cases < 0,1) and add a final adjustment if the result is beyond 10 (which is true for most cases > 1). The only left problem are values very close to the lower working limit (1E-499). Here the log10 may be returned as -499 so that a division by 10^-500 would result. This case is handled separately.

Code:
01 ABS 02 ENTER 03 X≠0? 04 LOG 05 IP 06 1 07 - 08 -499 09 X>Y? 10 X<>Y 11 RDN 12 10^x 13 / 14 10 15 X>Y? 16 SIGN 17 /

What about this one? Any errors or problematic values? At least my 35s handles all test cases correctly. And also all others I tried. Is this a solution or am I missing something?

Dieter
01-22-2014, 08:41 PM
Post: #10
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 12:23 PM)Gerson W. Barbosa Wrote:  This should be quite easy on the HP-15C: 9 steps on my first attempt.
I talked too soon. It wouldn't work for |x| < 0.01. Despite my bad fix attempt, which made it three times as large, it won't work for |x| < 10^-10.
Code:
 # -------------------------------------------- # HEWLETT·PACKARD 15C Simulator program # Created with version 3.3.00 # -------------------------------------------- # --------------------------------------------    000 {             }     001 {    42 21  0 } f LBL 0    002 {       43 36 } g LSTx    003 {          36 } ENTER    004 {       43 13 } g LOG    005 {       43 44 } g INT    006 {          16 } CHS    007 {           1 } 1    008 {          40 } +    009 {          13 } 10^x    010 {          20 } ×    011 {       44 36 } STO RAN#    012 {       45 36 } RCL RAN#    013 {    42 21 11 } f LBL A    014 {    43 30  2 } g TEST x<0    015 {          16 } CHS    016 {       44 36 } STO RAN#    017 {       45 36 } RCL RAN#    018 {           1 } 1    019 {           0 } 0    020 {          20 } ×    021 {           1 } 1    022 {          16 } CHS    023 {          34 } x<>y    024 {          40 } +    025 {    43 30  2 } g TEST x<0    026 {       22  0 } GTO 0    027 {       43 36 } g LSTx    028 {       43 32 } g RTN # --------------------------------------------
01-22-2014, 08:58 PM (This post was last modified: 01-22-2014 09:12 PM by Dieter.)
Post: #11
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 08:41 PM)Gerson W. Barbosa Wrote:  I talked too soon. It wouldn't work for |x| < 0.01. Despite my bad fix attempt, which made it three times as large, it won't work for |x| < 10^-10.

Here's a 15C-version of the 42s-solution I posted:
Code:
01 ABS 02 ENTER 03 X≠0? 04 LOG 05 INT 06 1 07 - 08 9 09 9 10 CHS 11 X>Y? 12 X<>Y 13 RDN 14 10^x 15 / 16 1 17 0 18 X>Y? 19 LOG 20 /

What do you think?

EDIT: Walter - yes, I eventually found this button with the red X on it. ;-)

Dieter
01-22-2014, 09:14 PM (This post was last modified: 01-22-2014 09:20 PM by Gerson W. Barbosa.)
Post: #12
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 08:58 PM)Dieter Wrote:
(01-22-2014 08:41 PM)Gerson W. Barbosa Wrote:  I talked too soon. It wouldn't work for |x| < 0.01. Despite my bad fix attempt, which made it three times as large, it won't work for |x| < 10^-10.

Here's a 15C-version of the 42s-solution I posted:
Code:
01 ABS 02 ENTER 03 X≠0? 04 LOG 05 INT 06 1 07 - 08 9 09 9 10 CHS 11 X>Y? 12 X<>Y 13 RDN 14 10^x 15 / 16 1 17 0 18 X>Y? 19 LOG 20 /

What do you think?
I think I am this kind of programmer, except that I didn't find a way to make that work as it should :-)

"Category 2: ENGINEER. This type insists on making the problem more
complicated than it really is. Engineers hang onto an idea
tenaciously until they find a way to make it work."

P.S.: Leaving for a meeting now. Will try it later.
01-22-2014, 09:40 PM
Post: #13
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 09:14 PM)Gerson W. Barbosa Wrote:  I think I am this kind of programmer
Ah, yes, I found this some years ago and there is some truth in it. ;-) Usually I like short and elegant solutions like "category 3", but there is also some, err... beauty in the category 4 and 6 versions. :-)

Dieter
01-23-2014, 12:40 AM
Post: #14
 Thomas Klemm Senior Member Posts: 1,610 Joined: Dec 2013
RE: [42S] MANT Challenge
Maybe a little boring:
Code:
00 { 39 Byte Prgm } 01 LBL "MANT" 02 X=0? 03 RTN 04 ABS 05 1 06 X<>Y 07 LBL 00 08 X>=Y? 09 GTO 01 10 10 11 * 12 GTO 00 13 LBL 01 14 10 15 X<>Y 16 LBL 02 17 X<Y? 18 RTN 19 10 20 / 21 GTO 02 22 END

Cheers
Thomas
01-23-2014, 02:52 AM (This post was last modified: 01-23-2014 03:15 AM by Gerson W. Barbosa.)
Post: #15
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 08:58 PM)Dieter Wrote:
(01-22-2014 08:41 PM)Gerson W. Barbosa Wrote:  I talked too soon. It wouldn't work for |x| < 0.01. Despite my bad fix attempt, which made it three times as large, it won't work for |x| < 10^-10.

Here's a 15C-version of the 42s-solution I posted:
Code:
01 ABS 02 ENTER 03 X≠0? 04 LOG 05 INT 06 1 07 - 08 9 09 9 10 CHS 11 X>Y? 12 X<>Y 13 RDN 14 10^x 15 / 16 1 17 0 18 X>Y? 19 LOG 20 /

What do you think?

Very nice! It passes all equivalent Werner's examples for the HP-15C and others I tried. So does the following, as far as I have tested:
Code:
    001 {    42 21 11 } f LBL A    002 {       43 20 } g x=0    003 {       43 32 } g RTN    004 {       43 16 } g ABS    005 {          36 } ENTER    006 {       43 13 } g LOG    007 {           1 } 1    008 {          30 } -    009 {       43 44 } g INT    010 {          13 } 10^x    011 {          10 } ÷    012 {       44 36 } STO RAN#    013 {       45 36 } RCL RAN#    014 {           1 } 1    015 {           0 } 0    016 {          20 } ×    017 {       43 32 } g RTN

What do you think? Still hanging on to the idea of using the RAN# register at some point in the program, but hoping to qualify for another category :-)

Gerson.
01-23-2014, 07:46 AM
Post: #16
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
@Dieter: congratulations are in order! The only thing I don't like is that it uses three stack levels (I have to complain about something)

@Thomas: that's a variant of one of my attempts:
Code:
 01*LBL "MANT" 02 ABS 03 10 04 X>Y? 05 GTO 02 06*LBL 01 07 STO/ ST Y 08 X<=Y? 09 GTO 01 10*LBL 02 11 STO* ST Y 12 X>Y? 13 GTO 02 14 / 15 RTN

Unfortunately, running time on a real 42S becomes prohibitive for larger exponents.
But you can easily improve on that ;-)
01-23-2014, 09:34 AM
Post: #17
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
It's always so much easier to improve upon someone else's code than to write your own.. 7 bytes shorter, and using only two stack levels:
Code:
 00 { 25-Byte Prgm } 01*LBL "MANT" 02 ABS 03 1        avoid upper exponent limit 04 X<Y? 05 10^X 06 / 07 ENTER 08 X#0? 09 LOG 10 IP 11 10^X 12 / 13 1        multiply by 10 if needed 14 X>Y? 15 10^X 16 * 17 END
01-23-2014, 10:47 AM
Post: #18
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-22-2014 12:38 PM)Werner Wrote:  Fails for 2e-02 and all 0<x<0.1 that are not 10^-n
Fails for 9.99999999999*10^n
The reason is that LOG(9.99999999999) = 1 exactly, on a real 42S
Werner
Code:
 00 { 28-Byte Prgm } 01>LBL "MANT" 02 X=0? 03 RTN 04 ABS 05 ENTER 06 LOG 07 1 08 - 09 IP 10 10^X 11 ÷ 12 10 13 X<>Y 14 X>=Y? 15 RCL÷ ST Y 16 .END.

Gerson.
01-23-2014, 11:02 AM (This post was last modified: 01-23-2014 11:02 AM by Werner.)
Post: #19
 Werner Senior Member Posts: 695 Joined: Dec 2013
RE: [42S] MANT Challenge
Hi Gerson!
Fails for 1 e-499, I'm afraid
Werner
01-23-2014, 11:05 AM
Post: #20
 Gerson W. Barbosa Senior Member Posts: 1,454 Joined: Dec 2013
RE: [42S] MANT Challenge
(01-23-2014 11:02 AM)Werner Wrote:  Hi Gerson!
Fails for 1 e-499, I'm afraid
Werner
So does the HP-15C version for 1e-99... I think I'll stick to the WP 34S and use MANT instead :-)

Gerso.
 « Next Oldest | Next Newest »

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