English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Floating-point classification
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-01-26 (21:10)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: Floating-point classification
> Is there a way to test whether a float has value 'nan' or 'inf' ? I
> didn't find in the standard library the equivalent of libc functions
> isfinite() or isnan() (those are macros I think). 

Right.  Eventually, similar functions should be added to OCaml.
Currently, OCaml provides all math functions specified in ANSI C
(the lowest common denominator of all platforms it supports), but
isfinite(), isnan() and fpclassify() are from ISO C9X, not ANSI C.

> I think that in C you can test if a float is nan by comparing it
> with itself. It seems to work in  OCaml too :

Yes, it works provided the argument to = is statically known to be of
type "float" (so that the float-specific equality operation is
performed instead of generic equality).  As for infinities, you can
also test equality with +inf or -inf.  The following functions should
do the job:

let isnan (x: float) = x <> x
let posinf = 1.0 /. 0.0
let neginf = -. posinf
let isposinf x = x = posinf
let isneginf x = x = neginf
let isfinite x = not(isnan x || isposinf x || isneginf x)

etc, etc.

- Xavier Leroy