>Ack.  I just re-read my own post, and it's not all that clear.  sorry.
>anybody feel like giving me some pointers?

Hmm, not sure what you mean by "cheating". I think these programs work in a
way similar to your algorithm.

Since the digits in the string values are characters, there's a bit of
extra fooling around that detracts from the "beauty" of the algorithm.

These functions are useful if you need to do integer arithmetic beyond
the precision REBOL is capable of. There's an extra kludge to allow a
decimal point in the longer argument. Still need to add the ability
to accept "negative" arguments.

Later,
Eric

==========

string-add: func [
    {add two string representations of integers numerically}
    a [string!] b [string!]
    /local c d
][
    a: copy a   b: copy b
    if (length? a) < (length? b) [c: b b: a a: c]
    insert/dup b "0" (length? a) - (length? b)
    a: tail a  b: tail b
    d: 0 while [ not head? b ] [
        a: back a  b: back b
        if #"." = first a [a: back a]
        c: (first a) + (first b) + d - 48
        d: either c > #"9" [c: c - 10  1][0]
        change a c
    ]
    if d > 0 [insert a "1"]
    a
]

string-subtract: func [
    {subtract two string representations of integers numerically}
    a [string!] b [string!]
    /full-length
    /local c d neg
][
    a: copy a   b: copy b
    neg: copy either any [
        (length? a) < (length? b)
        all [(length? a) = (length? b)  a < b]
    ][c: b b: a a: c "-"][""]
    insert/dup b "0" (length? a) - (length? b)
    a: tail a  b: tail b
    d: 0 while [ not head? b ] [
        a: back a  b: back b
        if #"." = first a [a: back a]
        c: to char! (first a) - (first b) - d + 48
        d: either c < #"0" [c: c + 10   1][0]
        change a c
    ]
    if not full-length [
        while [all [#"0" = first a  1 < length? a]][remove a]
    ]
    join neg a
]

Reply via email to