Version française
Home     About     Download     Resources     Contact us    
Browse thread
NaN Test in OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christian Lindig <lindig@e...>
Subject: NaN Test in OCaml

George Russell <ger@informatik.uni-bremen.de> has suggested on
comp.lang.ml the following test to find out whether a float is NaN:

        x is not a NaN <=> (x = x)

Doing this leads to interesting results with OCaml 3.0:

    # let nan x = not (x = x);;
    val nan : 'a -> bool = <fun>
    # 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>
    # 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.

-- Christian 
        
-- 
Christian Lindig          Harvard University - DEAS
lindig@eecs.harvard.edu   33 Oxford St, MD 242, Cambridge MA 02138
phone: +1 (617) 496-7157  http://www.eecs.harvard.edu/~lindig/