Could anybody explain this results?>sin(2*pi)-2.449213e-16 #should be zero>(10^16)*sin(log2(4)*pi)-2.449213 #should be zero too and explain what to do to correct this events? Thanks!!! Branimir K. Hackenberger
On Sunday 12 September 2004 13:28, Branimir K. Hackenberger wrote:> Could anybody explain this results? > > >sin(2*pi) > > -2.449213e-16 #should be zeroIt is, in the sense that> all.equal(sin(2*pi), 0)[1] TRUE> >(10^16)*sin(log2(4)*pi) > > -2.449213 #should be zero too > > > and explain what to do to correct this events?For starters, you need to invent and then use an infinite precision computer where the variable 'pi' is really the ratio between the circumference and radius of a circle, and not just a finite precision approximation of it. Good luck trying :-) Deepayan
Branimir K. Hackenberger <hack <at> ffos.hr> writes: : : Could anybody explain this results? : : >sin(2*pi) : -2.449213e-16 #should be zero : : : >(10^16)*sin(log2(4)*pi) : -2.449213 #should be zero too : : : and explain what to do to correct this events? Someone else has already explained why this is. In terms of what you can do, in general, you have to keep finite precision in mind when performing computer calculations using floating point representations. Sometimes there are tricks. If you know that the result or an intermediate result will be integer then if the error is sufficiently small you can round it at that point: R> round(sin(2*pi)) [1] 0 R> (10^16)*round(sin(log2(4)*pi)) [1] 0 If exact arithmetic is required you may need to use a symbolic mathematics package capable of exact arithmetic. There are both free, e.g. yacas, and commercial ones, e.g. mathematica, maple. For example, in yacas: In> Sin(2*Pi); Out> 0; In> (10^16)*Sin(IntLog(4,2)*Pi) Out> 0;
This is in the FAQs. It has to do with representation of floating point numbers. You can not represent 'pi' exactlly in the 53 bits of precision in floating point. If you notice, 2^-53 is 1.1e-16 which indicates the 'roundoff' is in the least significant bit of the precision; this is to be expected with floating point numbers. __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Branimir K. Hackenberger" To: <r-help at stat.math.ethz.ch> <hack at ffos.hr> cc: Sent by: Subject: [R] calculating error r-help-bounces at stat.m ath.ethz.ch 09/12/2004 14:28 Could anybody explain this results?>sin(2*pi)-2.449213e-16 #should be zero>(10^16)*sin(log2(4)*pi)-2.449213 #should be zero too and explain what to do to correct this events? Thanks!!! Branimir K. Hackenberger ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html