1

I want to use the trigonometric functions of math.h to use 0,1,-1 as sign identifiers for a plane coordinate system.

cos(pi)*length = -1*length
sin(pi)*length = 0

But the functions in math.h requires radian values while I have degree values. Although these can be converted in the formulas I don't know it it will affect the accuracy of the answers i.e. cos(pi) = -0.99999..

Will this affect the operations on my code?

EDIT: What can I do to get my desired results?

  • this might help a bit : `double pi = acos(-1);` – Sander De Dycker Mar 16 '16 at 15:04
  • If your degree-values have many decimal places, then yes, calculating pi*degrees/180 might lose precision. OTOH, it can't be exact anyway, I tend to believe that you won't be in trouble because of that. – Ctx Mar 16 '16 at 15:06
  • I don't actually need the exact values. The angles I will use are just 0, 90, 180, and 270 thus only need 0,1, and -1 as answers. – Carl Chester Ragudo Mar 16 '16 at 15:25

2 Answers2

1

They probably will affect the accuracy by the precision to which you have pi - usually one unit in the last place. For cos and sin, |d f(x)/dx| is less than one so your value should be within the same error. For tan, |d f(x)/dx| is not bounded so a small input change can create a large change of output.

Whether such small changes will affect the operation of your code depends largely on whether your code assumes than the results are exact or not. If your code makes faulty assumptions about floating point values, then it will fail, if it allows some small tolerance on equality comparisons, then it wont.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
1

Will this affect the operations on my code?

When conversion is exact between degree/radians, not a problem. Of course this only happens when the angle is +/- 0.0.

To use trig function and get exact trig function result (or at least nearly as good as one can get) with degree arguments, insure the degree argument is reduced (mod 360) first and then converted to radians. Further improvements can be had using trig identities: high precession and Sin and Cos

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256