1. Re: abs function

Message text written by Nick:
>These are the algorithms I use for abs (and sgn, another function I find=

>useful). They work for sequences as well as atoms.  I don't expect it's
>the fastest solution but should certainly be faster than invoking sqrt,
>as was suggested earlier.
>
>function sgn(object r)
>  return (r > 0) - (r < 0)
>end function
>
>function abs(object r)
>  return r * sgn(r)
>end function

Your sgn function (also suggested by LLH and others) seems to be about th=
e
fastest way to do a general object.  Here is a faster abs function (but n=
ot
as terse as yours).  Maybe someone can come up with an even faster abs or=

sgn function for a general object?

Colin Taylor

--start code
function abs(object obj)
    object val
    if atom(obj) then
        if obj < 0 then
            obj =3D -obj
        end if
    else
        for i =3D 1 to length(obj) do
            val =3D obj[i]
            if sequence(val) then
                obj[i] =3D abs(val)
            else
                if val < 0 then
                    obj[i] =3D -val
                end if
            end if
        end for
    end if
    return obj
end function
-- end code

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu