Hello everyone.
I need some help to understand when number precision in R is set. For this
please consider the following example
for (i in c(2:length(final))){
sizex <- c(sizex,(final[i]-final[i-1],digits=2))) # round is used to
remove
values that are too small like e-17.
print(round(final[i]-final[i-1],digits=2))
}
final[2]-final[1] return something like 4.440892e-16, which means that these two
numbers are the same. They are two but as they were derived from a different
process they are not the same for precision.
Also the line print(round(final[2]-final[1]),digits=2) returns 0 which is
correct
When the above loop stops executing inside sizex variable I find the value
4.440892e-16 which I was not expecting. As you can see from small code snippet
before setting the value in the sizex I try to round it. The print gives the
right value but for some reason it seems that inside the loop the precision in
sizex is changed.
Can you please help me clarify that?
Best Regards
Alex
[[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 18.10.2010 11:58:06:> Hello everyone. > I need some help to understand when number precision in R is set. Forthis> please consider the following example > > > for (i in c(2:length(final))){ > sizex <- c(sizex,(final[i]-final[i-1],digits=2))) # round is used toremove> values that are too small like e-17. > print(round(final[i]-final[i-1],digits=2)) > } > > final[2]-final[1] return something like 4.440892e-16, which means thatthese two> numbers are the same. They are two but as they were derived from adifferent> process they are not the same for precision. > > Also the line print(round(final[2]-final[1]),digits=2) returns 0 whichis> correct > > When the above loop stops executing inside sizex variable I find thevalue> 4.440892e-16 which I was not expecting. As you can see from small codesnippet> before setting the value in the sizex I try to round it. The print givesthe Not having your original data I presume you expect you are rounding when adding to sizex but you are not c(2,((2^0.5)^2,digits=2)) so either you forgot round or you are not telling us the whole story Regards Petr> right value but for some reason it seems that inside the loop theprecision in> sizex is changed. > > Can you please help me clarify that? > > Best Regards > Alex > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
FAQ 7.31 ?all.equal On Mon, Oct 18, 2010 at 5:58 AM, Alaios <alaios at yahoo.com> wrote:> Hello everyone. > I need some help to understand when number precision in R is set. For this > please consider the following example > > > ?for (i in c(2:length(final))){ > ? ?sizex <- c(sizex,(final[i]-final[i-1],digits=2))) # round is used to remove > values that are too small like e-17. > ? ?print(round(final[i]-final[i-1],digits=2)) > ?} > > final[2]-final[1] return something like 4.440892e-16, which means that these two > numbers are the same. They are two but as they were derived from a different > process they are not the same for precision. > > Also the line print(round(final[2]-final[1]),digits=2) returns 0 which is > correct > > When the above loop stops executing inside sizex variable I find the value > 4.440892e-16 which I was not expecting. ?As you can see from small code snippet > before setting the value in the sizex I try to round it. The print gives the > right value but for some reason it seems that inside the loop the precision in > sizex is changed. > > Can you please help me clarify that? > > Best Regards > Alex > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?