ios4 - Value of sine 180 is coming out as 1.22465e-16 -
i want implement sine , cosine calculator in ios4:
if([operation isequal:@"sin"]){ operand = (operand*m_pi/180.0); operand=sin(operand); }
the code gives me correct answer values 0 through 90.
when give value of 180, 1.22465e-16
answer. expect zero.
where small difference come from?
this caused inability of binary number system represent pi.
one possible solution use symmetry of sin:
sindeg(x) = sindeg(180 - x)
(or alternatively):
sin(x) = sin(m_pi - x)
transforming angle range (-pi/2 : pi/2) reduces error of approximation.
basically:
if([operation isequal:@"sin"]){ operand = fmod(operand, 360); if (operand > 270) operand -= 360; else if (operand > 90) operand = 180 - operand; operand=sin(operand*m_pi/180.0); }
Comments
Post a Comment