From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 897327EF37 for ; Thu, 23 Jul 2015 19:00:39 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of Xavier.Leroy@inria.fr) identity=pra; client-ip=212.27.42.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="Xavier.Leroy@inria.fr"; x-sender="Xavier.Leroy@inria.fr"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of Xavier.Leroy@inria.fr) identity=mailfrom; client-ip=212.27.42.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="Xavier.Leroy@inria.fr"; x-sender="Xavier.Leroy@inria.fr"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@smtp3-g21.free.fr) identity=helo; client-ip=212.27.42.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="Xavier.Leroy@inria.fr"; x-sender="postmaster@smtp3-g21.free.fr"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BNAQBNHbFVmwMqG9RcGYdcvWmCXgKBTkwBAQEBAQESAQEBAQEGCwsJIS6EJAEBAQMjMCURCxgCAgUWCwICCQMCAQIBRRMIAQGILrVAlhsMIIEiiiqEUzoXglKBQwWUYIdYhGKIT5BMhCWDOAEBAQ X-IPAS-Result: A0BNAQBNHbFVmwMqG9RcGYdcvWmCXgKBTkwBAQEBAQESAQEBAQEGCwsJIS6EJAEBAQMjMCURCxgCAgUWCwICCQMCAQIBRRMIAQGILrVAlhsMIIEiiiqEUzoXglKBQwWUYIdYhGKIT5BMhCWDOAEBAQ X-IronPort-AV: E=Sophos;i="5.15,532,1432591200"; d="scan'208";a="141009896" Received: from smtp3-g21.free.fr ([212.27.42.3]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 23 Jul 2015 19:00:39 +0200 Received: from [192.168.1.2] (unknown [82.237.71.191]) by smtp3-g21.free.fr (Postfix) with ESMTPS id EBDF2A61B4 for ; Thu, 23 Jul 2015 19:00:38 +0200 (CEST) Message-ID: <55B11DB6.9000908@inria.fr> Date: Thu, 23 Jul 2015 19:00:38 +0200 From: Xavier Leroy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: caml-list@inria.fr References: <20150723083517.GA2029@pl-59055.rocqadm.inria.fr> <55B104C9.1000207@inria.fr> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Comparing floats 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