<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/10/3133ffc353d5d33e5386f5e4c73b858f"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-14T14:56:56"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max"
  prev="2003/10/6a954bc9190c6a9a31a6dca1225057fe"
  next="2003/10/d2f71a4af6c422779e3e3b63ba42bcbf"
  prev-in-thread="2003/10/686468535e6100213e2e85bca8be51f1"
  next-in-thread="2003/10/48561de698e6c05397123c5888cc4dc1"
  prev-thread="2003/10/bb6652d3ee0174ff91bc648fa5125d46"
  next-thread="2003/10/6638aad0140dc99b2349c854da4948c3"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/686468535e6100213e2e85bca8be51f1"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-14T14:37:18"
  subject="[Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/3133ffc353d5d33e5386f5e4c73b858f"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-14T14:56:56"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/48561de698e6c05397123c5888cc4dc1"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-14T20:52:17"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/22b8f0ac4fb38072f78facd4bf9434a7"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-10-14T23:45:32"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/242867c4c7ffbfa1014a17fb343b34a9"
  from="Hendrik Tews &lt;tews@t...&gt;"
  author="Hendrik Tews"
  date="2003-10-16T17:29:55"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/10/0b43b6a621394f9ecc76084111d3253d"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-10-16T13:17:07"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/896e4961fd3e3f4a71d469b298ebc60e"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-16T14:01:29"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/0eef11bc0ed3dcaefa1de6d2feea29a3"
  from="Christophe TROESTLER &lt;debian00@t...&gt;"
  author="Christophe TROESTLER"
  date="2003-10-17T09:25:50"
  subject="[Caml-list] Test nan (was: Weird behavior with nan&apos;s and min/max)">
</msg>
</msg>
<msg 
  url="2003/10/a832440f4488e9cf40654b2ad77a5c76"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-16T21:40:46"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/dc6b874ec397bff1e2016e7a813019ca"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-16T21:50:35"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
</msg>
<msg 
  url="2003/10/c1eb0f38b646fdaf599a7528eb9c4ee6"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2003-10-16T22:52:06"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
</msg>
</msg>
<msg 
  url="2003/10/84ed68a791de5fdc1f6098100bd856e4"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-10-17T14:56:33"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/dd582ea4978aa94ec44762de52be023d"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-17T15:14:06"
  subject="Floating point exceptions (Was Re: [Caml-list] Weird behavior with  nan&apos;s and min/max)">
</msg>
<msg 
  url="2003/10/2547e7d7d785deed112b4caad2e8f5c2"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-10-17T23:56:05"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/ef4a5558eeba1a0b9901b9af7a62d4cf"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-10-20T13:29:23"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/ae21027a037cf89c74ed354226a8cb16"
  from="Yaron Minsky &lt;yminsky@c...&gt;"
  author="Yaron Minsky"
  date="2003-10-20T13:43:13"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
<msg 
  url="2003/10/3ed5948ec142822cf10a8e406fdc428b"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-10-20T14:24:47"
  subject="Re: [Caml-list] Weird behavior with nan&apos;s and min/max">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/10/84b26be77ae760b6c6d2291d12cde704"
  from="Jed Davis &lt;jdev@p...&gt;"
  author="Jed Davis"
  date="2003-10-17T15:54:27"
  subject="[Caml-list] Re: Weird behavior with nan&apos;s and min/max">
</msg>
</msg>
</thread>

<contents>
Here's a working solution, I think.

let raw_min = min
let raw_max = max

let nan_or_equal x y =
  not (x &lt; y) &amp;&amp; not (x &gt; y)

(** min and max that choose nan if its there *)
let min x y =
  if x = y then x
  else if nan_or_equal x y then nan
  else raw_min x y

let max x y =
  if x = y then x
  else if nan_or_equal x y then nan
  else raw_max x y


&gt; min and max have pretty strange behavior when it comes to nan's.  Here's
&gt; an example:
&gt;
&gt; # min 3. nan;;
&gt; - : float = 3.
&gt; # min  nan 3.;;
&gt; - : float = nan
&gt;
&gt; When you think about it, the reason for this is clear.  comparisons
&gt; involving nan's always return false, so if you simply implement min as
&gt; follows:
&gt;
&gt;    if x &lt; y then x else y
&gt;
&gt; the the result will depend on the order.
&gt;
&gt; Now here's the weird bit.  I decided I wanted a polymorphic comparison
&gt; that wouldn't have this problem.  But this is a little harder than it
&gt; seems, since it turns out that specialized float version of equality is
&gt; different from the polymorphic version.  Here's the example:
&gt;
&gt; # let raw_min = min
&gt; val raw_min : 'a -&gt; 'a -&gt; 'a = &lt;fun&gt;
&gt; # let min x y =
&gt;   if not (y = y) then y
&gt;   else if not (x = x) then x
&gt;   else raw_min x y
&gt; ;;
&gt;         val min : 'a -&gt; 'a -&gt; 'a = &lt;fun&gt;
&gt; # let fmin (x:float) y =
&gt;   if not (y = y) then y
&gt;   else if not (x = x) then x
&gt;   else raw_min x y
&gt; ;;
&gt;         val fmin : float -&gt; float -&gt; float = &lt;fun&gt;
&gt; # fmin 3. nan;;
&gt; - : float = nan
&gt; # fmin nan 3.;;
&gt; - : float = nan
&gt; # min nan 3.;;
&gt; - : float = nan
&gt; # min 3. nan;;
&gt; - : float = 3.
&gt;
&gt; So, does this count as a bug?  Any ideas about how to fix this behavior in
&gt; a reasonably efficient way?
&gt;
&gt; Yaron
&gt;
&gt; -------------------
&gt; To unsubscribe, mail caml-list-request@inria.fr Archives:
&gt; http://caml.inria.fr
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
&gt; http://caml.inria.fr/FAQ/
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt;

-------------------
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

</contents>

</message>

