Reputation: 4095
I am trying to calculate angle between three points and I need to use the Tangent function tan()
. The weird thing is that VBA return wrong value.
for example:
tan(209) = 0.554309051
but in VBA:
tan(209) = -.696695985548265
My friend told me about something called "Normalize". but I didn't understand what he's talking about and how to do it. Why is this happening?
Upvotes: 1
Views: 7965
Reputation: 1
It appears VB is like Excel where is assumes the input value for Tangent is in radians. If it is not in radians, you need to convert angles in degrees to radians.
In Excel, you have to use the RADIANS() function to convert your data from angles to radians. Use the DEGREES() function to convert back.
Upvotes: 0
Reputation: 38520
In addition to confusing radians and degrees, I think you may be confusing tangent and arctangent.
In a comment, you say you call Tan
like this: Math.Tan((A(2) - B(2)) / (B(1) - A(1)))
. That is a very atypical way to be supplying an angle argument to a tangent! And in another comment, you imply that you expect this to give you an angle (EDIT: or "radians"). But tangent won't give you an angle or "radians"!
I can't believe nobody else is pointing this out. This physicist is outraged.
Based on this, I believe that what you really want is arctangent, i.e. Math.Atn((A(2) - B(2)) / (B(1) - A(1)))
. That will give you an angle (in radians) when supplied the length ratio of the opposite to adjacent sides.
Of course, the above is largely speculative, because I don't know what you really are trying to accomplish, but from what I can tease out of the bits of implicit information sprinkled across your question and comments, that is indeed what I would put my money on.
Upvotes: 1
Reputation: 14685
(not addressing if using TAN is correct or not):
Perhaps your cell is formated in some special way and it's changing the value. In Excel 2007, both the worksheet funcion and VBA return -11.8641847236695 for tan(209). That's different from what you have above.
Upvotes: 1