enco ← base (##.ary) ratnum            ⍝ ⍺-ary representation of rational ⍵.

Returns a character vector representation of the ⍺-based rational number that is
⎕CT-tolerably close to ⍵.

        3 ary 66.6      ⍝ ternary representation of rational number.
    2110.12101210...

If [base] is a positive number in the range 2..36, then the result is a charact-
er vector, in one of three forms:

    nnn.fff             ⍝ exact rational with finite representation.
    nnn.fff...          ⍝ exact non-terminating rational.
    nnn.fff?            ⍝ inexact irrational number.

A rational number of the form P÷Q, expressed in base ⍺,  has a finite represent-
ation only if all of the prime factors of Q  are also prime factors of the base.
Otherwise,  the  ⍺-ary  representation  of P÷Q will have a repeating sequence of
trailing digits. Examples, base-10 are:

        10 ary 1÷ 2×3   ⍝ 3 is relatively prime to base (2×5).
    0.166...

        10 ary 4÷ 11    ⍝ 11 is relatively prime to base (2×5).
    0.3636...

The ellipsis (...)  identifies the _longest_ twice-repeated sequence to its left
as the repetition sequence. In the first example above, only the '6' is repeated
indefinitely, whereas in the second example, the whole of the longer string '36'
is repeated. See also →esh←.

        10 ary 16*¯3    ⍝ exact, finite representation.
    0.000244140625

        10 ary ÷7       ⍝ exact, rational with repeating sequence.
    0.142857142857...

        10 ary ○1       ⍝ inexact, irrational with truncated digits.
    3.1415926535898?

If left argument  [base]  is negative,  character vector formatting is supressed
and nested structure: (sig exp fix rep rat) is returned. Where:

    sig:    scalar  sign of [ratnum] (1, ¯1),
    exp:    scalar  base-⍺ exponent of [ratnum],
    fix:    vector  leading, fixed part of mantissa,
    rep:    vector  trailing, repeated part of mantissa,
    rat:    scalar  boolean: 1 if the repesentation is exact.

        ⍕ 369+÷7                ⍝ primitive ⍕-format of number (⎕pp=17).
    369.14285714285717

        10 ary 369+÷7           ⍝ [ary]-formatted number.
    369.142857142857...

        ¯10 ary 369+÷7          ⍝ [ary]-unformatted (sig exp fix rep rat)
    ┌─┬─┬─────┬───────────┬─┐
    │1│3│3 6 9│1 4 2 8 5 7│1│
    └─┴─┴─────┴───────────┴─┘

Technical notes:

The inner conditional formatting function is derived from the power operator ⍣:

    ofmt←{                              ⍝ optional formatting.
        ...
    }⍣(⍺>0)         ⍝ NB: ⍣(⍺>0)        ⍝ :: [char] ← ∇ (sig exp fix rep rat)
    ...
    q=1:ofmt sign(exp+1)(,1)⍬ 1         ⍝ base*n: special case.
    ofmt sign exp,⍬ digs p×base         ⍝ optional formatting of ⍺-ary number.

This is slightly neater than passing ⍺ and testing it in the first line:

    ofmt←{                              ⍝ optional formatting.
        ⍺>0:⍵                           ⍝ no formatting: done.
        ...
    }                                   ⍝ :: [char] ← ∇ (sig exp fix rep rat)
    ...
    q=1:⍺ ofmt sign(exp+1)(,1)⍬ 1       ⍝ base*n: special case.
    ⍺ ofmt sign exp,⍬ digs p×base       ⍝ optional formatting of ⍺-ary number.

or, worse, using guards to determine whether to apply the function:

    ofmt←{                              ⍝ optional formatting.
        ...
    }                                   ⍝ :: [char] ← ∇ (sig exp fix rep rat)
    ...
    z1←sign(exp+1)(,1)⍬ 1               ⍝ base*n: special case.
    zn←sign exp,⍬ digs p×base           ⍝ optional formatting of ⍺-ary number.
    rslt←(q=1)⊃zn z1                    ⍝ special or regular result.
    (⍺<0):rslt                          ⍝ -ive base: (sig exp fix rep rat)
    ofmt rslt                           ⍝ +ive base: 'nnn.fff...'


[ary] uses {⍵ 1÷1∨⍵} to find a rational number ⎕CT-tolerably close to its "real"
argument ⍵.  This means that the accuracy of the result is restricted to at most
(|⍺)⍟÷⎕CT digits.

If  [ary] fails to find a repetition sequence in the mantissa within this limit-
ing number of digits, it will assume the number is irrational.  In this case, it
reports a non-exact result by appending '?' to the formatted form or returning 0
as the "rat" item of the unformatted form.

        ⎕ct←1e¯14           ⍝ default comparison tolerance:

        10ary ÷17           ⍝ fails to find repetition sequence.
    0.058823529411765?

        ⎕ct←1e¯15           ⍝ finer comparison tolerance:

        10ary ÷17           ⍝ additional digit; still non-exact.
    0.0588235294117647?

        ⎕ct←1e¯16           ⍝ finer still comparison tolerance:

        10ary ÷17           ⍝ exact result with repeated sequence.
    0.058823529411764705882352941176470...
    ⍝  └──────────────┘└──────────────┘


        ⎕ct←1e¯14 ⋄ ¯10ary ÷17                  ⍝ ⎕CT=1E¯14: inexact.
    ┌─┬──┬───────────────────────────┬┬─┐
    │1│¯1│5 8 8 2 3 5 2 9 4 1 1 7 6 5││0│
    └─┴──┴───────────────────────────┴┴─┘

        ⎕ct←1e¯16 ⋄ ¯10ary ÷17                  ⍝ ⎕CT=1E¯16: exact.
    ┌─┬──┬┬───────────────────────────────┬─┐
    │1│¯1││5 8 8 2 3 5 2 9 4 1 1 7 6 4 7 0│1│
    └─┴──┴┴───────────────────────────────┴─┘

Examples:

    10 ary ÷27              ⍝ exact rational number (note repeated sequence).
0.0370370...

    10 ary 2*÷2             ⍝ irrational number (note trailing ? digit).
1.4142135623731?

    16 ary ○1               ⍝ (irrational) Pi in hex.
3.243F6A8885A?

    3ary 0.25               ⍝ one quarter in ternary.
0.02020...

    2ary 1.1                ⍝ finite decimal → repeating binary.
1.000110011...

    ¯3 ary ¯12345           ⍝ negative whole ternary number (exact).
┌──┬─┬───────────────┬┬─┐
│¯1│9│1 2 1 2 2 1 0 2││1│
└──┴─┴───────────────┴┴─┘

    ¯16 ary *1              ⍝ hexadecimal irrational (inexact).
┌─┬─┬───────────────────────────┬┬─┐
│1│1│2 11 7 14 1 5 1 6 2 8 10 15││0│
└─┴─┴───────────────────────────┴┴─┘

See also: adic esh ratrep bt phinary
See also: numbers

Back to: contents

Back to: Workspaces