Re: Getting atom values with gets()

new topic     » goto parent     » topic index » view thread      » older message » newer message

It's overkill time: :)

My entire Euphoria life (up til around late april) is available for
download (5 Megs) from:

    http://www.bigfoot.com/~cyrek/public/euphoria/crweuph.zip

My mathematics libraries are still available from my Euphoria page...

This being the one that pertains to this thread...:

--- basebag.e --- Converts strings of didgits in bases 2 .. 36 into atoms
--                ...and back again.

type numbase(integer x)
    return x >= 2 and x <= 36
end type

type char(integer ch)
    return ch >= 0 and ch <= 255
end type

function isdigit(char ch)
    return (ch >= '0' and ch <= '9')
end function

function isalphanumeric(char ch)
    return isdigit(ch)
        or (ch >= 'A' and ch <= 'Z')
end function

function char2digit(char ch, numbase base)
    integer out
    out = -1
    if isalphanumeric(ch) then
        out = ch - '0' - 7 * (ch >= 'A')
        if out >= base then
            out = -1
        end if
    end if
    return out
end function

function digit2char(integer digit, numbase base)
    char out
    out = ' '
    if digit >= 0 and digit < base then
        out = digit + 48 + 7 * (digit > 9)
    end if
    return out
end function

global function seq2atom(sequence s, numbase base)
    integer sign, digit
    atom int, frac, multiplier
    integer fp

    fp = 0
    int = 0
    frac = 0
    multiplier = 1
    sign = 1
    digit = -1

    for i = 1 to length(s) do
        digit = char2digit(s[i], base)

        if s[i] <= 32 or s[i] = ',' then
            -- do nothing
        elsif s[i] = '-' and sign != -1 then
            sign = -1
        elsif digit != -1 then
            if fp then
                multiplier = multiplier / base
                frac = frac + multiplier * digit
            else
                int = base * int + digit
            end if
        elsif s[i] = '.' and not fp then
            fp = 1
        else
            exit
        end if
    end for

    return sign*(int+frac)
end function

global function atom2seq(atom a, numbase base)
    sequence out, front
    integer digit, maxfpp, fppcount
    atom int, frac
    char ch

    maxfpp = 14 -- max possible without accidentally hitting 15 :)
    maxfpp = floor(maxfpp/(log(base)/log(10))+.5)
    fppcount = 0
    -- Base precision on the fact that most computers can cope with
    -- [see first definition of maxfpp] digits after the _decimal_
    -- (i.e. base *10*) point.
    -- This is then scaled to the current base
    -- e.g. base 10, 12 digits -> base 2, 40 digits -> base 16, 10 digits etc.

    out = {}
    front = {}
    frac = a

    if a < 0 then
        front = "-"
        frac = -a
    end if

    int = floor(frac)
    frac = frac - int

    if int = 0 then
        front = front & '0'
    end if

    while int > 0 do
        digit = remainder(int, base)
        int = floor(int / base)

        ch = digit2char(digit, base)
        if ch = 32 then
            return front & out
        end if

        fppcount = fppcount + 1
        out = ch & out
    end while

    if frac = 0 then
        return front & out
    end if

    out = out & '.'

    while frac < 1 and fppcount < maxfpp do
        frac = frac * base
        digit = floor(frac)
        frac = frac - digit

        ch = digit2char(digit,base)
        if ch = 32 then
            return front & out
        end if

        fppcount = fppcount + 1
        out = out & ch
    end while

    while out[length(out)] = '0' do
        out = out[1..length(out)-1]
    end while

    return front & out
end function

global function baseconv(numbase from, numbase to_, sequence x)
    return atom2seq(seq2atom(x, from), to_)
end function

global function base27toalpha(sequence a)
    return a + 47*(a='0') + 16*(a>='1'and a<='9') + 9*(a>='A'and a<='R')
end function

global function alphatobase27(sequence a)
    return a - 47*(a='_') - 16*(a>='A'and a<='I') - 9*(a>='J'and a<='Z')
end function


--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu