phinary←{⎕IO ⎕ML←0 1                ⍝ Phinary representation of numbers ⍵.
    ⍺←1                             ⍝ result formatted by default.
    Ø(1+5*÷2)÷2                    ⍝ Phi.
    ''≡0/∊⍵:{                       ⍝ char array: inverse: phinary → decimal.
        1<|≡⍵:∇¨⍵                   ⍝ nested: decode each.
        '¯'=⊃⍵:-∇ 1↓⍵               ⍝ -ive: negation of inverse of +ive.
        aØ⊥⎕D⍳⍵~'.'                ⍝ phi decode of ⍵.
        a÷Ø*(≢⍵∪'.')-1+(,⍵)⍳'.'     ⍝ adjusted by posn of phinary point.
    }⍵                              ⍝ ⍵ is char vect phinary number.
    0≠≡⍵:⍺ ∇¨⍵                      ⍝ higher rank, depth: encode each.
    ⍵<0:'¯',⍺ ∇-⍵                   ⍝ negative.
    num←⍵                           ⍝ target number.
    ⍺{                              ⍝ format of powers of phi.
        ⍺=0:⍵                       ⍝ no formatting: raw powers.
        ⍵≡⍬:,'0'                    ⍝ '0' better than '.'.
        fmt←⌷∘'01'∘⊂                ⍝ format '1010...'
        lft(⌽⍳0⌈1+⌈/⍵)∊⍵           ⍝ digits to the left of '.'
        rgt(¯1-⍳0⌈|⌊/⍵)∊⍵          ⍝   ..      ..  right   ..
        rgt∧.=0:fmt lft             ⍝ all +ive: no phinary point.
        lft∧.=0:'0.',fmt rgt        ⍝ all -ive: leading '0.'
        (fmt lft),'.',fmt rgt       ⍝ both: point-separated digits.
    }⍬{                             ⍝ accumulated powers of phi.
        num=Ø+.*⍺:⍺                 ⍝ convergence: done.
        (-⍴⍺)↑1                   ⍝ delta (1 in least sig place)
        num=Ø+.*⍺+:⍺+             ⍝ (⍺+∆) convergence: done.
        k←⌊Ø⍟⍵                      ⍝ next power of phi.
        (⍺,k)∇ ⍵-Ø*k                ⍝ accumlated powers of phi.
    }⍵
}

code_colours

test script

Back to: notes

Back to: Workspaces