caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Comparing floats
Date: Thu, 23 Jul 2015 19:00:38 +0200	[thread overview]
Message-ID: <55B11DB6.9000908@inria.fr> (raw)
In-Reply-To: <CABbVA-DenGsjVLhgX3TRugd873FhFy7PsePLO16hUVokb6ov8g@mail.gmail.com>

On 23/07/15 18:34, Boris Yakobowski wrote:
> I got confused when negating OCaml's '=' operator (a dangerous side-effect
> of writing too much C!), hence the erroneous '!=' instead of <>. Regarding,
> 'nan <> nan', I had convinced myself that all comparisons with NaN returned
> false. I wonder why <>/!= got a special treatment since e.g. 'nan < 1.' and
> '1. < nan' both return false.

Perhaps so that (using Caml syntax) "x <> y" is always equivalent to
"not (x = y)".  But I agree it can be viewed as yet another oddity
with NaNs.

Just to clarify my previous post:

  let compare_float (x: float) (y: float) = compare x y

gives a total order over floats that behaves sensibly, if somewhat
arbitrarily, over NaN arguments:  two NaNs are equal, and any NaN is
less than any non-NaN float.  In contrast,

let compare_float (x: float) (y: float) =
  if x < y then -1 else if x > y then 1 else 0

is not a proper order, because the implied equality is not transitive.
Consider:

   compare_float 0.0 nan = 0
   compare_float nan 1.0 = 0
   compare_float 0.0 1.0 = -1

So, don't use the latter definition for e.g. sorting a list of floats
that can contain NaN.  The former definition is more robust.

- Xavier Leroy

      reply	other threads:[~2015-07-23 17:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-23  8:35 Sébastien Hinderer
2015-07-23  8:46 ` Francois Berenger
2015-07-23  9:05   ` Mr. Herr
2015-07-23  9:01 ` Xavier Leroy
2015-07-23  9:35   ` Sébastien Hinderer
2015-07-23  9:54   ` Mr. Herr
2015-08-04  9:06     ` Goswin von Brederlow
2015-07-23 11:34 ` Boris Yakobowski
2015-07-23 15:14   ` Jacques-Henri Jourdan
2015-07-23 16:34     ` Boris Yakobowski
2015-07-23 17:00       ` Xavier Leroy [this message]

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=55B11DB6.9000908@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    /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).