<?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/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"
  prev="2003/10/938f70fcc22cb66da66abce64e1ebd62"
  next="2003/10/896e4961fd3e3f4a71d469b298ebc60e"
  prev-in-thread="2003/10/242867c4c7ffbfa1014a17fb343b34a9"
  next-in-thread="2003/10/896e4961fd3e3f4a71d469b298ebc60e"
  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>
&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.

Yes, it's a long-standing bug for which we haven't yet a good
solution.  More exactly, there are two problematic solutions:

1- Fix polymorphic equality so that it behaves like IEEE equality on floats,
i.e. it always returns false when one of its arguments is NaN.
The problem is that this breaks the implication
        x == y  imply  x = y
and thus the current implementation of polymorphic equality needs to
be made less efficient.  Currently, x = y starts by testing x == y
and returns true if the pointer equality holds.  But this could be the
wrong result according to the new specification, since x can contain
an NaN somewhere.  Hence, polymorphic equality would have to traverse
its two arguments even when they are physically the same.  The
performance impact of this change on real programs is unknown.

2- As J M Skaller proposed, change the behavior of polymorphic
equality and its version specialized to floats so that nan = nan
and nan &lt;&gt; x if x &lt;&gt; nan.  Similar changes need to be done on the
&lt;&gt;, &lt;= and &gt;= tests for consistency.  IEEE comparisons would then have to be
provided as separate primitives.  This preserves the implication
x == y ==&gt; x = y.  But the machine code generated for =, &lt;&gt;, &lt;= and &gt;=
over floats will have to be a lot less efficient than it is now, since
all processors implement float comparisons as per IEEE.

Coming back to your proposed workaround:

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

A way to make this work would be to replace the "not (x = x)" tests
by calls to the following function (of type 'a -&gt; bool):

let is_obj_nan x =
  Obj.tag (Obj.repr x) = Obj.double_tag &amp;&amp;
  (let f = (Obj.magic x : float) in not (f = f))

Not pretty, I agree.

- Xavier Leroy

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

