[erlang-questions] Inf, NaN, and -Inf

Jesse Gumm gumm@REDACTED
Mon Feb 27 18:36:01 CET 2012


Maybe I'm misinterpreting your requirements, but if lists:min and
lists:max don't work for you (ie, you want to do it in one pass), you
could do this:

minmax([]) -> undefined;
minmax([First|Rest]) ->
    lists:foldl(fun(X,{Min,Max}) ->
        if
            X < Min -> {X,Max};
            X > Max -> {Min,X};
            true -> {Min,Max}
        end
    end,{First,First},Rest).

> minmax:minmax([1,2.45e34,3,4,567567,-657,7,584545.76]).
{-657,2.45e34}

-Jesse

On Mon, Feb 27, 2012 at 11:07 AM, Tom Burdick <thomas.burdick@REDACTED> wrote:
> So in python and C to find the biggest and smallest number in a list
> of floating point numbers I'd do something like a lists:foldl and
> simply give it
>
> inf and -inf respectively as the values to compare against, or better
> yet use some built in defines or variables defining what the min and
> max values for a particular number type are.
>
> Is there some sane way to do this in erlang? Is there a way to get the
> floating point values inf and -inf in erlang or the maximal and
> minimal values for integers and floats in erlang? I didn't see it in
> my hunt around the documentation perhaps I just missed it.
>
> Thanks!
>
> Tom
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



-- 
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm



More information about the erlang-questions mailing list