Post Reply 
(48G/49/50) Conversion To and From Base i-1
02-25-2023, 09:10 PM (This post was last modified: 02-25-2023 09:11 PM by John Keith.)
Post: #1
(48G/49/50) Conversion To and From Base i-1
Complex base i-1 enables positive and negative integers, and Gaussian integers, to be represented by a nonnegative integer. More information in this paper.

The programs listed here convert between integers or complex numbers and their base i-1 representation. The programs are in the form of a directory because there are several sub-programs and several dependencies.

The directory includes the programs I→DL and DL→I from this thread. These two programs can be removed from this directory if they are already in your path.

The programs I→Bi1 and Bi1→I are for the conversion of positive or negative integers. They are short and fast because they take advantage of simple bit patterns. The programs Z→Bi1 and Bi1→Z are for the conversion of complex numbers (or imaginary numbers in the form of (0, n)). These programs are considerably larger and more.. complex. Smile They are based on the Python programs here. The program Bi1ADD, which is used as a sub-program, adds two numbers encoded in base i-1 without needing to convert them first.

Code:

DIR
  I\->Bi1
  \<< -4. I\->DL 1.            @ Convert to base -4
    \<< DUP 1. > { 10. + } IFT @ Change 2, 3 to 12, 13
    \>> DOLIST 16. DL\->I      @ Interpret as base 16
  \>>
  Bi1\->I
  \<< 16. I\->DL 1.            @ Results will be meaningless unless
    \<< DUP 1. > { 10. - } IFT @ all base 16 digits are in {0, 1, 12, 13}
    \>> DOLIST -4. DL\->I
  \>>
  Z\->Bi1
  \<< 0. RND C\->R SWAP DUP
    :: I\->Bi1 { DROP 0. } IFTE SWAP @ Convert real part if not 0
    IF DUP
    THEN DUP 0. <
      { NEG 7. } 3. IFTE Bi1IM       @ Convert imaginary part if not 0
    ELSE DROP
    END
  \>>
  Bi1\->Z
  \<< 2. I\->DL (-1.,1.) DL\->I @ Convert to base 2, interpret as base -1+i
  \>>
  Bi1ADD
  \<< 64. STWS 2340. # 0h 0. \-> sum sh
    \<<
      WHILE PICK3 PICK3 OR
        { 2340. 2258. } PICK3 POS NOT OR
      REPEAT { 2560. 1976. 2048. 1464. }
        OVER 2. / IP 4. MOD 1. + GET
        SWAP 8. / IP + PICK3 2. MOD +
        OVER 2. MOD + sum OVER 2. MOD
        2. sh ^ * R\->B OR 'sum' STO
        'sh' 1. STO+ ROT 2. / IP ROT 2. / IP ROT
      END DROP DROP2 sum B\->R
    \>>
  \>>
  Bi1IM
  \<<
    WHILE OVER
    REPEAT OVER 2. MOD
      { ROT OVER Bi1ADD ROT ROT }
      IFT SWAP 2. / IP SWAP DUP Bi1ADD
    END DROP2
  \>>
  I\->DL
  \<< DUP ABS SWAP 0 < DEPTH 3. - \-> b f d
    \<<
      WHILE b DUP2 / FLOOR ROT ROT MOD
        DEPTH d - ROLLD DUP
      REPEAT f
        :: NEG IFT
      END DROP DEPTH d - \->LIST
    \>>
  \>>
  DL\->I
  \<< \-> b
  \<< OBJ\->
    IF DUP 1. >
    THEN 1 SWAP 2. SWAP
      START b * ROT OVER * ROT + SWAP
      NEXT
    END DROP
  \>>
\>>
END
Find all posts by this user
Quote this message in a reply
Post Reply 




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