Hi,

I agree -- and my examples using round were meant as bad and dangerous
examples. Using round at the last step is better and may solve the problem,
but in your example ...

> round(8.8-7.8,1)==1
[1] TRUE

... you have to know in advance how many decimal places can possibly make a
difference (is it just one? maybe it is 2? 3? 15?).

> round(8.8-7.8,14)==1
[1] TRUE
> round(8.8-7.8,15)==1
[1] FALSE

... or, equivalently,

> 8.8-7.8-1 < 1e-15
[1] TRUE
> 8.8-7.8-1 < 1e-16
[1] FALSE

Best regards,
Kenn

On Thu, Dec 11, 2008 at 4:18 PM, Bernardo Rangel Tura
<tura@centroin.com.br>wrote:

>
> Hi Kenn,
>
> Well I think your use or round isn't optimal solution. If you using
>
> round(x,1)-round(x,1) you create 2 problems
>
> First: error propagation because you make 2 round.
>
> Second: you don't using "guard digits" approach.
>
> The optimal use of round is using in last calculation:
>
> Look this
>
> > round(8.8,1)-round(7.8,1)>1
> [1] TRUE
> > round(8.8-7.8,1)>1
> [1] FALSE
> > round(8.8-7.8,1)==1
> [1] TRUE
>
> Bernardo Rangel Tura, M.D,MPH,Ph.D
> National Institute of Cardiology
> Brazil
>
> ---------- Original Message -----------
> From: "Kenn Konstabel" <lebatsnok@gmail.com>
> To: "emma jane" <emma_me_jane@yahoo.com>
> Cc: R help <R-help@stat.math.ethz.ch>
> Sent: Thu, 11 Dec 2008 11:53:01 +0200
> Subject: Re: [R] Logical inconsistency
>
> > Rounding can do no good because....
> >
> > round(8.8,1)-round(7.8,1)>1
> > # still TRUE
> > round(8.8)-round(7.7)>1
> > # FALSE
> >
> > What you might do is compute a-b-1 and compare it to a very small number:
> >
> > (8.8-7.8-1) < 1e-10
> > # TRUE
> >
> > K
> >
> > On Wed, Dec 10, 2008 at 11:47 AM, emma jane <emma_me_jane@yahoo.com>
> > wrote:
> >
> > > Thanks Greg, that does make sense.Ā  And I've solved the problem by
> > > rounding the variables before taking the difference between them.
> > >
> > > Thanks to all who replied.
> > >
> > > Emma JaneĀ
> > >
> > >
> > >
> > >
> > > ________________________________
> > > From: Greg Snow <Greg.Snow@imail.org>
> > >
> > > .com.br>; Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk@idi.ntnu.no>;
> Chuck
> > > Cleland <ccleland@optonline.net>
> > > Cc: R help <R-help@stat.math.ethz.ch>
> > > Sent: Tuesday, 9 December, 2008 16:30:08
> > > Subject: RE: [R] Logical inconsistency
> > >
> > > Some (possibly all) of those numbers cannot be represented exactly, so
> > > there is a chance of round off error whenever you do some arithmetic,
> > > sometimes the errors cancel out, sometimes they don't.Ā  Consider:
> > >
> > > > print(8.3-7.3, digits=20)
> > > [1] 1.000000000000001
> > > > print(11.3-10.3, digits=20)
> > > [1] 1
> > >
> > > So in the first case the rounding error gives a value that is slightly
> > > greater than 1, so the greater than test returns true (if you round the
> > > result before comparing to 1, then it will return false).Ā  In the
> second
> > > case the uncertainties cancelled out so that you get exactly 1 which is
> not
> > > greater than 1 an so the comparison returns false.
> > >
> > > Hope this helps,
> > >
> > > --
> > > Gregory (Greg) L. Snow Ph.D.
> > > Statistical Data Center
> > > Intermountain Healthcare
> > > greg.snow@imail.org
> > > 801.408.8111
> > >
> > >
> > > > -----Original Message-----
> > > > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-
> > > > project.org] On Behalf Of emma jane
> > > > Sent: Tuesday, December 09, 2008 7:02 AM
> > > > To: Bernardo Rangel Tura; Wacek Kusnierczyk; Chuck Cleland
> > > > Cc: R help
> > > > Subject: Re: [R] Logical inconsistency
> > > >
> > > > Many thanks for your help, perhaps I should have set my query in
> > > > context .... !
> > > >
> > > > I'm simply calculating an indicator variable [0,1] based on the
> whether
> > > > the difference between two measured variables is > 1 or <=1.
> > > >
> > > > I understand the FAQ about floating point arithmetic, but am still
> > > > puzzled that it only apparently applies to certain elements, as
> > > > follows:
> > > >
> > > > 8.8 - 7.8 > 1
> > > > > TRUE
> > > >
> > > > 8.3 - 7.3 > 1
> > > > > TRUE
> > > >
> > > > However,
> > > >
> > > > 10.2 - 9.2 > 1
> > > > >FALSE
> > > >
> > > > 11.3 - 10.3>1
> > > > >Ā FALSE
> > > >
> > > > Emma Jane
> > > >
> > > >
> > > >
> > > >
> > > > ________________________________
> > > > From: Bernardo Rangel Tura <tura@centroin.com.br>
> > > > To: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk@idi.ntnu.no>
> > > > Cc: R help <R-help@stat.math.ethz.ch>
> > > > Sent: Saturday, 6 December, 2008 10:00:48
> > > > Subject: Re: [R] Logical inconsistency
> > > >
> > > > On Fri, 2008-12-05 at 14:18 +0100, Wacek Kusnierczyk wrote:
> > > > > Berwin A Turlach wrote:
> > > > > > Dear Emma,
> > > > > >
> > > > > > On Fri, 5 Dec 2008 04:23:53 -0800 (PST)
> > > >
> > > > > >
> > > > > >
> > > > > >> Please could someone kindly explain the following
> inconsistencies
> > > > > >> I've discovered__when performing logical calculations in R:
> > > > > >>
> > > > > >> 8.8 - 7.8 > 1
> > > > > >>
> > > > > >>> TRUE
> > > > > >>>
> > > > > >> 8.3 - 7.3 > 1
> > > > > >>
> > > > > >>> TRUE
> > > > > >>>
> > > > > >
> > > > > > Gladly:Ā  FAQ 7.31
> > > > > >
> http://cran.at.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-
> > > > th
> > > > > > ink-these-numbers-are-equal_003f
> > > > > >
> > > > > >
> > > > >
> > > > > well, this answer the question only partially.Ā  this explains why
> a
> > > > > system with finite precision arithmetic, such as r, will fail to be
> > > > > logically correct in certain cases.Ā  it does not explain why r, a
> > > > > language said to isolate a user from the underlying
> implementational
> > > > > choices, would have to fail this way.
> > > > >
> > > > > there is, in principle, no problem in having a high-level language
> > > > > perform the computation in a logically consistent way.Ā  for
> example,
> > > > > bc is an "arbitrary precision calculator language", and has no
> > > > problem
> > > > > with examples as the above:
> > > > >
> > > > > bc <<< "8.8 - 7.8 > 1"
> > > > > # 0, meaning 'no'
> > > > >
> > > > > bc <<< "8.3 - 7.3 > 1"
> > > > > # 0, meaning 'no'
> > > > >
> > > > > bc <<< "8.8 - 7.8 == 1"
> > > > > # 1, meaning 'yes'
> > > > >
> > > > >
> > > > > the fact that r (and many others, including matlab and sage,
> perhaps
> > > > > not
> > > > > mathematica) does not perform logically here is a consequence of
> its
> > > > > implementation of floating point arithmetic.
> > > > >
> > > > > the faq you were pointed to, and its referring to the goldberg's
> > > > > article, show that r does not successfully isolate a user from
> > > > details
> > > > > of the lower-level implementation.
> > > > >
> > > > > vQ
> > > >
> > > > Well, first of all for 8.-7.3 is not equal to 1 [for computers]
> > > >
> > > > > 8.3-7.3-1
> > > > [1] 8.881784e-16
> > > >
> > > > But if you use only one digit precision
> > > >
> > > > > round(8.3-7.3,1)-1
> > > > [1] 0
> > > > > round(8.3-7.3,1)-1>0
> > > > [1] FALSE
> > > > > round(8.3-7.3,1)==1
> > > > [1] TRUE
> > > >
> > > >
> > > > So the problem is the code write and no the software
> > > >
> > > > --
> > > > Bernardo Rangel Tura, M.D,MPH,Ph.D
> > > > National Institute of Cardiology
> > > > Brazil
> > > >
> > > >
> > > >
> > > >Ā  Ā  Ā  Ā  [[alternative HTML version deleted]]
> > >
> > >
> > >
> > >        [[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.
> > >
> > >
> >
> >       [[alternative HTML version deleted]]
> ------- End of Original Message -------
>
>

	[[alternative HTML version deleted]]

