[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | T. Kurt Bond <tkb@t...> |
| Subject: | RE: [Caml-list] float number |
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..
Asking CMU Common Lisp (with *read-default-float-format* set to
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