try this
0.3-0.1 == 0.2
[1] FALSE
round(0.3-0.1,1)==round(0.2,1)
[1] TRUE
liov
2009/4/21 Marc Schwartz marc_schwa...@me.com
On Apr 21, 2009, at 5:55 AM, Duncan Murdoch wrote:
On 21/04/2009 3:48 AM, Petr PIKAL wrote:
Hi
r-help-boun...@r-project.org napsal dne 20.04.2009 19:01:46:
wolfgang.siewert wolfgang.siewert at gmail.com writes:
There is a way around: round(0.7-0.3,1)==0.4
(TRUE)
Obviously there is a problem with some combinations of decimal
subtractions,
that - we have the feeling - shouldt be solved.
Oh no, not that one again! This was lecture two in my first computer
course in 1968, but it seems to be gone the way of the dodo since than.
Maybe that is because of Excel is so widespread now and gives expected
results (it probably silently rounds all decimal numbers before
calculation).
I don't have Excel, but I expect OpenOffice duplicates its bugs pretty
well. And in OpenOffice I see all sorts of bugs due to this, e.g. examples
where x = y and y = z but x != z, cases where I can calculate a number like
1 + 4.e-15 and get something different from 1, but if I enter it directly as
1.004, it gets changed to 1.
So it only gives expected results in some tests, not others.
Duncan Murdoch
As Dieter noted from our offlist exchange, this had been discussed
previously back in 2003. Just to refresh memories:
https://stat.ethz.ch/pipermail/r-help/2003-June/034565.html
https://stat.ethz.ch/pipermail/r-help/2003-June/034860.html
OO.org has replicated Excel's behavior to a fault. Thus:
Spreadsheet Use - Brain to Porridge
Just to update OO.org's behavior using version 3.0.1 on OSX:
Formula: =4.145 * 100 + 0.5 Result: 415.
Formula: =0.5 - 0.4 - 0.1 Result: 0.
Formula: =(0.5 - 0.4 - 0.1) Result: 0.
So nothing has changed in OO.org in five years. Somebody with Excel 2007
might want to try the 2nd and 3rd formula examples to see if using parens
still makes a difference in the result as compared to the formula without
the parens.
FWIW, now that I am on OSX, I can add the following output using Numbers
'09:
Formula: =4.145 * 100 + 0.5 Result: 415.
Formula: =0.5 - 0.4 - 0.1 Result: -2.77556E-17
Formula: =(0.5 - 0.4 - 0.1) Result: -2.77556E-17
It does look like R's behavior has changed since then. Using:
R version 2.9.0 Patched (2009-04-18 r48348)
on OSX:
# This first example has changed.
# Prior result was 414.94
print(4.145 * 100 + 0.5, digits = 20)
[1] 415
formatC(4.145 * 100 + 0.5, format = E, digits = 20)
[1] 4.14943157E+02
print(0.5 - 0.4 - 0.1, digits = 20)
[1] -2.77555756156289e-17
formatC(0.5 - 0.4 - 0.1, format = E, digits = 20)
[1] -2.77555756156289135106E-17
What is interesting is that:
4.145 * 100 + 0.5 == 415
[1] FALSE
(4.145 * 100 + 0.5) - 415
[1] -5.684342e-14
all.equal(4.145 * 100 + 0.5, 415, 0)
[1] Mean relative difference: 1.369721e-16
So it would appear that in the first R example above, the print() function
has changed in a material fashion.
HTH,
Marc Schwartz
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Luis Iván Ortiz Valencia
Estatístico Msc.
...
Curriculum Lattes
http://buscatextual.cnpq.br/buscatextual/visualizacv.jsp?id=K4778724J3
...
http://oplanetaliov.blogspot.com/
...
[[alternative HTML version deleted]]
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.