From ripley at stats.ox.ac.uk  Tue Feb 19 18:30:17 2008
From: ripley at stats.ox.ac.uk (ripley at stats.ox.ac.uk)
Date: Tue, 19 Feb 2008 18:30:17 +0100 (CET)
Subject: [Rd] Incorrect paste() output (PR#10801)
Message-ID: <20080219173017.9A953282EFF6@mail.pubhealth.ku.dk>

  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--27464147-598378112-1203442115=:3205
Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed
Content-Transfer-Encoding: 8BIT

On Tue, 19 Feb 2008, asfe at di.fc.ul.pt wrote:

> Hello,
>
> I am writing this message because of an incorrect output by paste().
> Please try the following script to see if the evidence I collected is
> reproducible:
>
> x <- c(10152, 28177);
> y <- c(9576, 26625);
> d <- y - x;
>
> d;
> [1]  -576 -1552
>
> paste(d, collapse = ", ");
> [1] "-576, -1552"
>
> x <- x / 1000;
> y <- y / 1000;
> d <- y - x;
>
> d;
> [1] -0.576 -1.552
>
> paste(d, collapse = ", ");
> [1] "-0.575999999999999, -1.552"
>
> The output of the last paste() is incorrect. I am using R version 2.6.1.

Why do you think it is incorrect?

?paste says

      'paste' converts its arguments (_via_ 'as.character') to character
      strings, and concatenates them (separating them by the string
      given by 'sep').

and

> as.character(d)
[1] "-0.575999999999999" "-1.552"

so this is correctly documented.  From as.character()

      'as.character' represents real and complex numbers to 15
      significant digits (technically the compiler's setting of the ISO
      C constant 'DBL_DIG', which will be 15 on machines supporting
      IEC60559 arithmetic according to the C99 standard).

and note

> print(d, digits=15)
[1] -0.575999999999999 -1.552000000000000

If you want a limited number of significant figures in paste(), use 
format() to convert the number yourself.

This is another manifestation of FAQ Q7.31: computer calculations on 
non-representable numbers are subject to rounding and representation 
errors.


> --
> Ant?nio Ferreira - http://www.di.fc.ul.pt/~asfe
> Assistant Lecturer - Department of Informatics
> Faculty of Sciences - University of Lisbon - PT
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
--27464147-598378112-1203442115=:3205--



[Rd] Incorrect paste() output (PR#10801)
ripley at stats.ox.ac.uk