caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Mentre <David.Mentre@inria.fr>
To: Christian Lindig <lindig@eecs.harvard.edu>
Cc: Caml Mailing List <caml-list@inria.fr>,
	George Russell <ger@informatik.uni-bremen.de>,
	Archisman Rudra <archi@mosaic.mrl.nyu.edu>
Subject: Re: NaN Test in OCaml
Date: 01 Feb 2001 10:19:47 +0100	[thread overview]
Message-ID: <qtlg0hyu5e4.fsf@pochi.inria.fr> (raw)
In-Reply-To: Christian Lindig's message of "Wed, 31 Jan 2001 14:05:03 -0500"

Hi Christian,

I'm far from being at the level of Caml implementors but:

Christian Lindig <lindig@eecs.harvard.edu> writes:

>     # let nan x = not (x = x);;
>     val nan : 'a -> bool = <fun>
                ^^ here you have a polymorphic equality test
>     # nan (1.0 /. 0.0);;
>     - : bool = false            (* correct *)
>     # nan (0.0 /. 0.0);;
>     - : bool = false            (* should be true *)
> 
> The following definition of nan uses a type annotation and has a
> different result:
> 
>     # let nan (x:float) = not (x = x);;
>     val nan : float -> bool = <fun>
                ^^^^^ here we know that we have floats
>     # nan (0.0 /. 0.0);;
>     - : bool = true             (* correct *)
>     # nan (1.0 /. 0.0);;
>     - : bool = false            (* correct *)
> 
> Is this a bug or a feature? Anyway, I guess this again shows the subtleties
> of equality.

I would say a feature. As Xavier said in his mail[1], if the compiler
knows that in *every case* we have a float, it generates a float
specific comparison code. Otherwise, the compiler takes the safe way by
using a generic comparison operator.

You can see this with an undocumented option of the bytecode compiler:

pochi(mentre):~ [976] ocaml -dinstr
        Objective Caml version 3.00

# let nan x = not (x = x);;
[...]
	ccall equal, 2
[...]
val nan : 'a -> bool = <fun>

# let nan (x:float) = not (x = x);;
[...]
	ccall eq_float, 2
[...]
val nan : float -> bool = <fun>


If you look at assembly code, you'll see the same behavior:

pochi(mentre):/tmp [1008] cat nan.ml
let nan_poly x = not (x = x)

let nan_float (x:float) = not (x = x)

pochi(mentre):/tmp [1009] ocamlopt -S nan.ml
pochi(mentre):/tmp [1010] cat nan.s
[...]
Nan_nan_poly_43:
.L100:
	pushl	%eax
	pushl	%eax
	movl	$equal, %eax
	call	caml_c_call
        ^^^^^^^^^^^^^^^^^^^ the compiler call a generic
                            (i.e. polymorphic equality operator)
[...]
Nan_nan_float_45:
	subl	$8, %esp
.L104:
	fldl	(%eax)
	fldl	(%eax)
	fucompp
        ^^^^^^^ float specific comparison operation, with NaN handling,
                see [2]
[...]


Hope it clarifies,
david

[1] http://caml.inria.fr/archives/200101/msg00195.html
[2] http://webster.cs.ucr.edu/Page_asm/ArtofAssembly/CH14/CH14-5.html#HEADING5-22
-- 
 David.Mentre@inria.fr -- http://www.irisa.fr/prive/dmentre/
 Opinions expressed here are only mine.



  reply	other threads:[~2001-02-02 15:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-01-31 19:05 Christian Lindig
2001-02-01  9:19 ` David Mentre [this message]
2001-02-01  9:58 ` Andreas Rossberg
2001-02-01 14:41 ` Xavier Leroy

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=qtlg0hyu5e4.fsf@pochi.inria.fr \
    --to=david.mentre@inria.fr \
    --cc=archi@mosaic.mrl.nyu.edu \
    --cc=caml-list@inria.fr \
    --cc=ger@informatik.uni-bremen.de \
    --cc=lindig@eecs.harvard.edu \
    /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).