[Rd] bug? (PR#8074)

berwin@maths.uwa.edu.au berwin at maths.uwa.edu.au
Wed Aug 17 04:59:18 CEST 2005


G'day Paul,

>>>>> "PM" == Paul Mosquin <pmosquin at 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

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   
School of Mathematics and Statistics        +61 (8) 6488 3383 (self)      
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin



More information about the R-devel mailing list