Post Reply 
(28/48/50) Mixed-Radix conversion programs
02-05-2023, 07:49 PM (This post was last modified: 02-05-2023 07:51 PM by John Keith.)
Post: #1
(28/48/50) Mixed-Radix conversion programs
These programs facilitate the conversion of numbers to and from mixed-radix bases. The inspiration came from this post by Thomas Klemm, as well as several related commands from the Wolfram Language (Mathematica).

The first program, MXRAD is similar to the Mathematica command IntegerDigits with the MixedRadix option. See the 3rd example here. Level 2 should have the number to be converted; level 1 should have a list of the radices ordered from most significant to least. For the 49g and 50g in exact mode, the phrase DUP2 / FLOOR ROT ROT MOD can be replaced with IDIV2, allowing the use of arbitrarily large integers.

Code:

\<< LIST\-> 1. + \-> n
  \<< n ROLL 2. n              @ Bring level 2 number down
    START SWAP                 @ Next radix
      DUP2 / FLOOR ROT ROT MOD @ Approximate IDIV2
      n ROLLD                  @ Store remainder
    NEXT n ROLLD n \->LIST     @ Store quotient and make list
  \>>
\>>

The second program, MX→N, is the inverse of the program above. It is similar to the Mathematica command FromDigits with the MixedRadix option. The level 2 object should be a list of the form returned by MXRAD; the level 1 object should be a list of radices such as the level 1 list given to MXRAD. The level 2 list should have one more element than the level 1 list.

Code:

\<< 1. + 0. OVER SIZE 1. SWAP
  FOR k 3. PICK k GET + OVER k GET *
  NEXT ROT ROT DROP2
\>>

The third program, DCOMP is similar to the Mathematica command NumberDecompose. However, the final 1 in the level 1 list is not used. As one can see, this is very similar to MXRAD except that the radices in the MXRAD list are the partial sums of the factors in the list for DCOMP. The number on level 2 need not be an integer- see the first example in the previous link.

Code:

\<< LIST\-> 1. + \-> n
  \<< n ROLL n 2.              @ Bring level 2 number down
    FOR k k ROLL               @ Bring next factor down
      DUP2 / FLOOR ROT ROT MOD @ Approximate IDIV2
      SWAP k ROLLD -1.         @ Store quotient
    STEP n \->LIST
  \>>
\>>

The final program, NCOMP, is the inverse of DCOMP. It is similar to the Mathematica command NumberCompose. As before, the level 2 list is of the form returned by DCOMP. The level 1 list is of the form given to DCOMP, again without the final 1.

Code:

\<< 1. + 0. OVER SIZE 1. SWAP
  FOR k OVER k GET 4. PICK k GET * +
  NEXT ROT ROT DROP2
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 




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