I've opened http://dev.clojure.org/jira/browse/CLJ-868

"The min and max functions in clojure.core behave unpredictably when
one or more of their arguments is Float/NaN or Double/NaN. This is
because the current implementation assumes that > provides a total
ordering, but this is not the case when NaN is added to the mix. This
is an unfortunate fact of life when dealing with IEEE floating point
numbers."

It seems to me that there are four approaches one might take to
address this.

1. Document the current undefined behavior of min and max in the
   presence of NaN.
2. Define that min and max will return NaN if any argument is NaN.
3. Define that min and max will ignore any NaN arguments.
4. Define that min and max will throw an exception if any argument is NaN.

1 Document current behavior as undefined
----------------------------------------

This requires no changes in the implementation, but it doesn't strike
me as a desirable resolution. Why unnecessarily codify clearly
confusing behavior?

2 Make NaN contagious
---------------------

Define min and max to return NaN if and only if at least one of their
arguments is NaN. This seems most in keeping with the (admittedly
perverse) behavior of NaN as specified.

See JLS 4.2.4 Floating Point Operations:

# An operation that overflows produces a signed infinity, an operation
# that underflows produces a denormalized value or a signed zero, and
# an operation that has no mathematically definite result produces
# NaN. All numeric operations with NaN as an operand produce NaN as a
# result. As has already been described, NaN is unordered, so a
# numeric comparison operation involving one or two NaNs returns false
# and any != comparison involving NaN returns true, including x!=x
# when x is NaN.

3 Let min and max ignore NaN arguments
--------------------------------------

This means that (min a NaN b) would be exactly equivalent to (min a
b). It would further imply that (min NaN) would be equivalent to
(min), which currently throws an exception.

4 Let NaN cause min and max to throw an exception
-------------------------------------------------

Currently min and max throw an exception if given arguments that are
not Numeric. One might plausibly argue that NaN is not numeric.

--

I've attached a patch with test and implementation for variant 2 to
the issue.

// Ben

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to