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: Andreas Rossberg <rossberg@p...>
Subject: Re: NaN Test in OCaml
Christian Lindig wrote:
> 
> 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 *)

Right, because the first example uses polymorphic equality which is
purely structural. The second uses proper floating point comparison.

Actually, what we have here, is a subtle kind of overloading. Subtle in
particular because it is overlapping. IMHO it would be preferable if
floating point comparison used different syntax, probably "=.". In that
case the compiler should probably emit a warning whenever he discovered
the use of structural equality on floats.

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

:: be declarative. be functional. just be. ::