Post Reply 
(49G) Convert Integer from Integer Base A to Integer Base B
01-06-2023, 07:02 PM
Post: #7
RE: (49G) Convert Integer from Integer Base A to Integer Base B
A modification of the program in post #1 which allows negative bases. The first half looks much shorter but actually takes the same number of bytes as the first half of the original program.

Code:

\<< DUP ABS 1 ROT 0 < :: NEG IFT \-> a b f
  \<<                     @ f = -1 if base is negative else 1
    \<< SWAP a * +
    \>> STREAM { } SWAP
    DO b IDIV2 ROT + SWAP
      f * DUP NOT         @ Negate quotient if base is negative
    UNTIL
    END DROP
  \>>
\>>

Also an approximate number version which is larger but runs on any RPL calculator. The first half is similar to my program DL→I in this thread.

Code:

\<< DUP ABS 1 ROT 0 <
  \<< NEG
  \>> IFT \-> a b f                           @ Must use program for HP-28
  \<< OBJ\-> 1 SWAP 2. SWAP
    START a * ROT OVER * ROT + SWAP
    NEXT DROP { } SWAP                        @ No REVLIST or STREAM
    DO b DUP2 / FLOOR ROT ROT MOD ROT + SWAP  @ Nor UNROT
     f * DUP NOT
    UNTIL
    END DROP
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (49G) Convert Integer from Integer Base A to Integer Base B - John Keith - 01-06-2023 07:02 PM



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