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

Richard O'Keefe ok@REDACTED
Mon Feb 27 21:47:51 CET 2012


On 28/02/2012, at 6:07 AM, Tom Burdick 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.

That is a VERY BAD way to do it in C or Python.
The maximum or minimum of an empty set of numbers is UNDEFINED.
It also has the exceedingly unpleasant property that
the reported maximum or minimum of a list might not even be an
element of that list!

If you want the maximum or minimum of a list in Erlang,
just use
	Max = lists:max(List)
or	Min = lists:min(List)

You will find the code in .../stdlib/src/lists.erl
and you will notice that it crashes, just as it should,
if List is empty.
   
> 
> 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

I haven't found one.  Somebody might want to write an EEP recommending
richer access to IEEE arithmetic features.  (Is anyone running Erlang
on z/Series?)

> or the maximal and
> minimal values for integers

The Erlang integer 10 is analogous to the Python integer 10L (note the
L suffix).  Try 10L**10000 in Python.  Now, what is the maximal and
what the minimal integer (of that kind) in Python?  Right; the limit
is basically "how much memory do you have?"  Same for Erlang.

I guess that means your technique for finding the maximum or minimum
number in a list doesn't actually work for Python either.

By the way, the function you want is not foldl but foldl1:

foldl1(F, [X|Xs]) -> foldl(F, X, Xs).





More information about the erlang-questions mailing list