trigonometry - R: Strange trig function behavior -


as matlab user transitioning r, have ran across problem of applying trigonometric functions degrees. in matlab there trig functions both radians , degrees (e.g. cos , cosd, respectively). r seems include functions radians, requiring me create own (see below)

cosd<-function(degrees) {   radians<-cos(degrees*pi/180)   return(radians) } 

unfortunately function not work of time. results shown below.

> cosd(90) [1] 6.123234e-17 > cosd(180) [1] -1 > cosd(270) [1] -1.836970e-16 > cosd(360) [1] 1 

i'd understand causing , how fix this. thanks!

this floating point arithmetic:

> all.equal(cosd(90), 0) [1] true > all.equal(cosd(270), 0) [1] true 

if meant "does not work properly"?

this faq: http://cran.r-project.org/doc/faq/r-faq.html#why-doesn_0027t-r-think-these-numbers-are-equal_003f


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -