Thanks to all who replied.

I'm not surprised that R uses floating point arithmetics, nor am I surprised that an object and it's printed version may differ due to e.g. rounding taking place in the print() function.

I am, however, surprised of the fact that seq() prints it's internal representation of 0 as 0.0 when `from = 0.5', but as -1.110223e-16 when `from = 0.7'.

//Henric


At 13:17 2004-09-03 +0100, you wrote:
On Fri, 3 Sep 2004, Roger Bivand wrote:

> On Fri, 3 Sep 2004, Henric Nilsson wrote:
>
> > Hi everyone,
> >
> > I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1
> > Patched and R 2.0.0 on Windows 2000, and the results are consistent.
> >
> > > seq(0.5, 0, by = -0.1)
> > [1] 0.5 0.4 0.3 0.2 0.1 0.0
> >
> > > seq(0.7, 0, by = -0.1)
> > [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 3.000000e-01
> > 2.000000e-01 1.000000e-01 -1.110223e-16
> >
> > Is this really the intended behaviour? I ended up using
>
> Well, you are using a floating point representation in a digital computer,
> so I don't think you should be surprised. Note that the internal
> representation is also modified by the print() functions, so what you see
> when an object is printed is not always exactly what is inside the object.
>
> >
> > > seq(0.7, 0, length = 8)
> > [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0
> >
> > which does what I want.


To expand a little, the code is in seq.default.

seq(0.7, 0, by = -0.1) is done by from + (0:n) * by for n=8
seq(0.7, 0, length = 8) is done by
  c(from, from + (1:(length.out - 2)) * by, to))

so the last value is handled differently.  Since 0.1 cannot be represented
exactly on a binary computer,

> print(7*0.1, digits=16)
[1] 0.7000000000000001

and you are seeing the result of 0.7 - 7*0.1.

--
Brian D. Ripley,                  [EMAIL PROTECTED]
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

______________________________________________
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to