caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@gmail.com>
To: Caml Mailing List <caml-list@inria.fr>
Subject: [Caml-list] min, max and nan
Date: Wed, 14 Jul 2004 23:51:41 -0400	[thread overview]
Message-ID: <891bd3390407142051653aae76@mail.gmail.com> (raw)

One of the oddities associated with NaNs in ocaml is what happens when
you take the min of something and nan.  Witness:

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

Surprisingly, the result depends on the ordering.  Why it does so is
clear once you look at the implementation and ponder for a moment the
IEEE requirements on comparisons involving nan.  When using ocaml
3.07, I tried and failed to come up with a polymorphic min that
behaved reasonably in this case, that is, that returned nan when
either argument is nan.  With 3.08, this is now doable.  Here's how it
works:

# let contains_nan x = x <> x;;
val contains_nan : 'a -> bool = <fun>
# let nmin x y = 
  if contains_nan x then x 
  else if contains_nan y then y 
  else min x y;;
      val nmin : 'a -> 'a -> 'a = <fun>
# nmin 3. nan;;
- : float = nan
# nmin nan 3.;;
- : float = nan

Still, we don't quite escape from the oddities of nan.  We still get
order dependence in the case of larger data structures that include
nan's:

# nmin (1,nan) (2,nan);;
- : int * float = (1, nan)
# nmin (2,nan) (1,nan);;
- : int * float = (2, nan)

But in my mind, the current state of affairs is a big improvement.

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


             reply	other threads:[~2004-07-15  3:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15  3:51 Yaron Minsky [this message]
2004-07-15 12:38 ` Markus Mottl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=891bd3390407142051653aae76@mail.gmail.com \
    --to=yminsky@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=yminsky@cs.cornell.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).