Re: [Rd] bug? (PR#8074)

From: <berwin_at_maths.uwa.edu.au>
Date: Wed 17 Aug 2005 - 02:59:18 GMT


G'day Paul,

>>>>> "PM" == Paul Mosquin <pmosquin@hotmail.com> writes:

    PM> Thomas, Thank you for your response.  I checked the FAQ and it
    PM> provides an example using sqrt(2) where you'd expect numerical
    PM> precision to be an issue.

You should expect numerical precision issues in all calculations involving "real" numbers in finite precision arithmetic. Perhaps the FAQ should include a link to, say:

        http://docs.sun.com/source/806-3568/ncg_goldberg.html

(A copy of Goldberg's "What Every Computer Scientist Should Know About Floating-Point Arithmetic".)

    PM> I guess that I expect R to act pretty much as C or C++ would     PM> do if I were to program the same code. Well, did you? I am pretty sure that you would have the same results in C or C++, at least I do (see below). But please, don't go and fill another bug report for your C or C++ compiler when you see that it behaves the same way as R. :-)

[bossiaea:test]$ cat test1.c
#include<stdlib.h>
#include<stdio.h>

int
main()
{
  double x = 0.2;

  if( x*x == 0.04)
    printf("The numbers are equal\n");
  else
    printf("The numbers are *NOT* equal\n");

  return EXIT_SUCCESS;
}
[bossiaea:test]$ gcc -o test1 test1.c
[bossiaea:test]$ ./test1
The numbers are *NOT* equal

    PM> It's a bit of a surprise that assignment of rationals, well
    PM> within precision, followed by multiplication leading to a
    PM> result well within precision picks up those extra bits along
    PM> the way.  Something to watch out for, to be sure.
No surprise at all for anybody who learned about how numbers are represented in a computer and how finite precision arithmetic works. Big hint, 0.2 is not exactly representable in a binary basis (and there have been function posted to the R mailing lists that show you the binary representation of real numbers on your machine).

Read the link above or a book on numerical analysis, e.g. Chapter 1 in

@Book{gentle:98:na,

  author =       {Gentle, J. E.},
  title =        {Numerical Linear Algebra with Applications in Statistics},
  publisher =    {Springer-Verlag},
  year =         1998,
  series =       {Statistics and Computing},
  address =      {New York}

}

Cheers,

        Berwin


R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Aug 17 13:02:36 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 22:27:38 GMT