This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] float number
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2002-09-05 (08:00) From: Vincent Barichard Subject: RE: [Caml-list] float number
```Thank you everyone, I know what I do wrong now.
I'll correct the test between float numbers (with another epsilon value).

Vincent

On Wed, 4 Sep 2002, T. Kurt Bond wrote:

> Vincent Barichard writes:
> > Hi,
> >
> > I've observed a strange behaviour of ocaml :
> >
> > let x = asin (-0.587527525714) in (x,x = (-0.628));;
> > - : float * bool = (-0.628, false)
> >
> > Why x doesn't equal to -0.628 ??
> >
> > Thanks
>
> Because x isn't equal to -0.628, even though it prints as -0.628.  The
> output routine doesn't print the value of x accurately.  The same
> thing happens in C.  O'Caml uses the C routine sprintf to format
> floating point numbers (see the function format_float in
> byterun/floats.c), and inherits poor float-printing routines from the
> C runtime library..
>
> DOUBLE-FLOAT, since O'Caml represents floating point numbers as
> doubles) for the value of (asin -0.587527525714) gives
> -0.6280000000001337, as does MzScheme.
>
> Here's a short (and non-portable) program that shows the problem
> occuring in C:
>
>     #include <stdio.h>
>     #include <math.h>
>
>     int
>     main (int argc, char **argv)
>     {
>       double x = asin (-0.587527525714);
>       double y = -0.628;
>
>       int *xp = &x;
>       int *yp = &y;
>
>       printf ("x: %g, x == (-0.628): %d\n", x, x == (-0.628));
>       printf ("*xp: %x *(xp+1): %x\n", *xp, *(xp+1));
>       printf ("*yp: %x *(yp+1): %x\n", *yp, *(yp+1));
>
>       exit (1);
>     }
>
> On FreeBSD 4.6 running on a Pentium II I compiled it like this:
>
>     cc -O -pipe   x.c -lm -o x
>
> and the output was:
>
>     x: -0.628, x == (-0.628): 0
>     *xp: 74bc6f33 *(xp+1): bfe41893
>     *yp: 74bc6a7f *(yp+1): bfe41893
>
> Notice that *xp and *yp are not equal.
>
> More information on print floating point numbers can be found in the
> paper "Printing Floating-Point Numbers Quickly and Accurately"; see
>
>     http://citeseer.nj.nec.com/28233.html
>
> --
> T. Kurt Bond, tkb@tkb.mpl.com
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

Vincent Barichard
Métaheuristiques et Optimisation Combinatoire
Faculté des Sciences d'Angers
Tel : 02 41 73 52 06
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```