caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Floating-point classification
@ 2001-01-22 11:42 Olivier Andrieu
  2001-01-25 16:05 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Olivier Andrieu @ 2001-01-22 11:42 UTC (permalink / raw)
  To: caml-list

Hello, 

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). 

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 :

        Objective Caml version 3.00

# let a = 0. /. 0. ;;
val a : float = nan
# a = a ;;
- : bool = false

Is it correct ? What about 'inf' ?

   Olivier



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Floating-point classification
  2001-01-22 11:42 Floating-point classification Olivier Andrieu
@ 2001-01-25 16:05 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2001-01-25 16:05 UTC (permalink / raw)
  To: Olivier Andrieu; +Cc: caml-list

> 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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-01-26 21:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-22 11:42 Floating-point classification Olivier Andrieu
2001-01-25 16:05 ` Xavier Leroy

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).