caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "T. Kurt Bond" <tkb@tkb.mpl.com>
To: Vincent Barichard <barichar@info.univ-angers.fr>
Cc: caml-list@pauillac.inria.fr
Subject: RE: [Caml-list] float number
Date: Wed, 4 Sep 2002 08:55:31 -0400	[thread overview]
Message-ID: <15734.707.339302.596392@tkb.mpl.com> (raw)
In-Reply-To: <Pine.GSO.4.44.0209031649490.25303-100000@helios.info-ua>

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


  reply	other threads:[~2002-09-04 12:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-03 14:57 Vincent Barichard
2002-09-04 12:55 ` T. Kurt Bond [this message]
2002-09-04 13:46   ` Vincent Barichard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15734.707.339302.596392@tkb.mpl.com \
    --to=tkb@tkb.mpl.com \
    --cc=barichar@info.univ-angers.fr \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).