* [Caml-list] float number
@ 2002-09-03 14:57 Vincent Barichard
2002-09-04 12:55 ` T. Kurt Bond
0 siblings, 1 reply; 3+ messages in thread
From: Vincent Barichard @ 2002-09-03 14:57 UTC (permalink / raw)
To: CAML-LIST
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
Vincent
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] float number
2002-09-03 14:57 [Caml-list] float number Vincent Barichard
@ 2002-09-04 12:55 ` T. Kurt Bond
2002-09-04 13:46 ` Vincent Barichard
0 siblings, 1 reply; 3+ messages in thread
From: T. Kurt Bond @ 2002-09-04 12:55 UTC (permalink / raw)
To: Vincent Barichard; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] float number
2002-09-04 12:55 ` T. Kurt Bond
@ 2002-09-04 13:46 ` Vincent Barichard
0 siblings, 0 replies; 3+ messages in thread
From: Vincent Barichard @ 2002-09-04 13:46 UTC (permalink / raw)
To: CAML-LIST
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..
>
> 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
>
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-09-05 8:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-03 14:57 [Caml-list] float number Vincent Barichard
2002-09-04 12:55 ` T. Kurt Bond
2002-09-04 13:46 ` Vincent Barichard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox