caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Weird behavior with nan's and min/max
@ 2003-10-14 14:37 Yaron Minsky
  2003-10-14 14:56 ` Yaron Minsky
                   ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Yaron Minsky @ 2003-10-14 14:37 UTC (permalink / raw)
  To: caml-list

min and max have pretty strange behavior when it comes to nan's.  Here's
an example:

# min 3. nan;;
- : float = 3.
# min  nan 3.;;
- : float = nan

When you think about it, the reason for this is clear.  comparisons
involving nan's always return false, so if you simply implement min as
follows:

   if x < y then x else y

the the result will depend on the order.

Now here's the weird bit.  I decided I wanted a polymorphic comparison
that wouldn't have this problem.  But this is a little harder than it
seems, since it turns out that specialized float version of equality is
different from the polymorphic version.  Here's the example:

# let raw_min = min
val raw_min : 'a -> 'a -> 'a = <fun>
# let min x y =
  if not (y = y) then y
  else if not (x = x) then x
  else raw_min x y
;;
        val min : 'a -> 'a -> 'a = <fun>
# let fmin (x:float) y =
  if not (y = y) then y
  else if not (x = x) then x
  else raw_min x y
;;
        val fmin : float -> float -> float = <fun>
# fmin 3. nan;;
- : float = nan
# fmin nan 3.;;
- : float = nan
# min nan 3.;;
- : float = nan
# min 3. nan;;
- : float = 3.

So, does this count as a bug?  Any ideas about how to fix this behavior in
a reasonably efficient way?

Yaron

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2003-10-20 14:24 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-14 14:37 [Caml-list] Weird behavior with nan's and min/max Yaron Minsky
2003-10-14 14:56 ` Yaron Minsky
2003-10-14 20:52   ` Yaron Minsky
2003-10-14 23:43     ` skaller
2003-10-16 17:29       ` Hendrik Tews
2003-10-16 13:16 ` Xavier Leroy
2003-10-16 14:01   ` Yaron Minsky
2003-10-17  9:26     ` [Caml-list] Test nan (was: Weird behavior with nan's and min/max) Christophe TROESTLER
2003-10-16 21:40   ` [Caml-list] Weird behavior with nan's and min/max Yaron Minsky
2003-10-16 21:50     ` Yaron Minsky
2003-10-16 22:52     ` Damien Doligez
2003-10-17 14:55   ` skaller
2003-10-17 15:14     ` Floating point exceptions (Was Re: [Caml-list] Weird behavior with nan's and min/max) Yaron Minsky
2003-10-17 23:55     ` [Caml-list] Weird behavior with nan's and min/max Yaron M. Minsky
2003-10-20 13:29       ` Xavier Leroy
2003-10-20 13:43         ` Yaron Minsky
2003-10-20 14:24           ` Xavier Leroy
2003-10-16 23:55 ` [Caml-list] " Jed Davis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).