Hi all,
Is there any problem of precision when using seq?. For example:
x- seq(0,4,0.1)
x[4]=0.3
BUT:
x[4]-0.3=5.551115e-17
It means when I use this condition within an if clause, it does not find
values with 0.3 for x[4] as it is not precisely 0.3.
Is there any bug in seq() ?
--
View this
Le lundi 18 juin 2012 à 12:58 -0700, hamoreno a écrit :
Hi all,
Is there any problem of precision when using seq?. For example:
x- seq(0,4,0.1)
x[4]=0.3
BUT:
x[4]-0.3=5.551115e-17
It means when I use this condition within an if clause, it does not find
values with 0.3 for x[4] as
Please read R FAQ 7.31. The problem is with your computer (and
everyone else's), and has nothing to do with seq().
On Mon, Jun 18, 2012 at 3:58 PM, hamoreno hamor...@asu.edu wrote:
Hi all,
Is there any problem of precision when using seq?. For example:
x- seq(0,4,0.1)
x[4]=0.3
BUT:
No, this is rather the nature of floating point calculations.
You may perhaps be looking for ?all.equal or R FAQ 7.31 (I think that's the
one) which is google-able. It's a complicated subject, but those should get you
started.
Best,
Michael
On Jun 18, 2012, at 2:58 PM, hamoreno
Thanks everyone... Seems that I will have to use round before seq to make
sure everything has the correct precision.
--
View this message in context:
http://r.789695.n4.nabble.com/Inconsistency-using-seq-tp4633739p4633750.html
Sent from the R help mailing list archive at Nabble.com.
Nope. round() is not failsafe either. Basically, there is theoretically no
way to guarantee immunity to floating point error in conditional
comparisons. You need to either switch to integers -- which **are** exactly
represented -- or figure out another way.
Of course, practically speaking,
actually, you should build your sequences with integers and scale those to get
floating point sequences.
---
Jeff NewmillerThe . . Go Live...
DCN:jdnew...@dcn.davis.ca.us
FYI,
isZero - function(x, neps=1, eps=.Machine$double.eps, ...) {
+ (abs(x) neps * eps)
+ }
x - seq(from=0, to=4, by=0.1)
isZero(x[4]-0.3)
[1] TRUE
isZero(x[4]-0.3, neps=1)
[1] TRUE
isZero(x[4]-0.3, neps=0.1)
[1] FALSE
You could also have called isZero() isValueSmallEnoughForWhatINeed().
On Mon, Jun 18, 2012 at 12:58:13PM -0700, hamoreno wrote:
Hi all,
Is there any problem of precision when using seq?. For example:
x- seq(0,4,0.1)
x[4]=0.3
BUT:
x[4]-0.3=5.551115e-17
It means when I use this condition within an if clause, it does not find
values with 0.3 for x[4]
9 matches
Mail list logo