<?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/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"
  prev="2003/10/242867c4c7ffbfa1014a17fb343b34a9"
  next="2003/10/dc6b874ec397bff1e2016e7a813019ca"
  prev-in-thread="2003/10/0eef11bc0ed3dcaefa1de6d2feea29a3"
  next-in-thread="2003/10/dc6b874ec397bff1e2016e7a813019ca"
  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>
I tried a solution based on Xavier's suggestion, and it now looks to me
like Xavier's suggsetion (or my implementation) is unsafe.  Here's the
code I tried:

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

let min x y =
  if is_obj_nan x then x
  else if is_obj_nan y then y
  else Pervasives.min x y

let max x y =
  if is_obj_nan x then x
  else if is_obj_nan y then y
  else Pervasives.max x y;;

The resulting code segfaulted on me, and the segfault went away when I
went back to the ordinary min and max.  Does anyone have a thought on
this?  Is this use of Obj safe or not?

I'm still trying to debug the actual location of the segfault, but the
lack of stacktraces makes it a bit harder....

y

&gt;&gt; Now here's the weird bit.  I decided I wanted a polymorphic comparison
&gt;&gt; that wouldn't have this problem.  But this is a little harder than it
&gt;&gt; seems, since it turns out that specialized float version of equality is
&gt;&gt; different from the polymorphic version.
&gt;
&gt; Yes, it's a long-standing bug for which we haven't yet a good
&gt; solution.  More exactly, there are two problematic solutions:
&gt;
&gt; 1- Fix polymorphic equality so that it behaves like IEEE equality on
&gt; floats,
&gt; i.e. it always returns false when one of its arguments is NaN.
&gt; The problem is that this breaks the implication
&gt;         x == y  imply  x = y
&gt; and thus the current implementation of polymorphic equality needs to
&gt; be made less efficient.  Currently, x = y starts by testing x == y
&gt; and returns true if the pointer equality holds.  But this could be the
&gt; wrong result according to the new specification, since x can contain
&gt; an NaN somewhere.  Hence, polymorphic equality would have to traverse
&gt; its two arguments even when they are physically the same.  The
&gt; performance impact of this change on real programs is unknown.
&gt;
&gt; 2- As J M Skaller proposed, change the behavior of polymorphic
&gt; equality and its version specialized to floats so that nan = nan
&gt; and nan &lt;&gt; x if x &lt;&gt; nan.  Similar changes need to be done on the
&gt; &lt;&gt;, &lt;= and &gt;= tests for consistency.  IEEE comparisons would then have to
&gt; be
&gt; provided as separate primitives.  This preserves the implication
&gt; x == y ==&gt; x = y.  But the machine code generated for =, &lt;&gt;, &lt;= and &gt;=
&gt; over floats will have to be a lot less efficient than it is now, since
&gt; all processors implement float comparisons as per IEEE.
&gt;
&gt; Coming back to your proposed workaround:
&gt;
&gt;&gt; # let raw_min = min
&gt;&gt; val raw_min : 'a -&gt; 'a -&gt; 'a = &lt;fun&gt;
&gt;&gt; # let min x y =
&gt;&gt;   if not (y = y) then y
&gt;&gt;   else if not (x = x) then x
&gt;&gt;   else raw_min x y
&gt;&gt; ;;
&gt;
&gt; A way to make this work would be to replace the "not (x = x)" tests
&gt; by calls to the following function (of type 'a -&gt; bool):
&gt;
&gt; let is_obj_nan x =
&gt;   Obj.tag (Obj.repr x) = Obj.double_tag &amp;&amp;
&gt;   (let f = (Obj.magic x : float) in not (f = f))
&gt;
&gt; Not pretty, I agree.
&gt;
&gt; - Xavier Leroy
&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>

