Standard Trignometry Functions
VB.Net offers a variety on Trignometry functions, they can be found in the Math. namespace.
Sine, Cosine and Tangent
Pocket calculators, astronomy books and formula lists often measure angles in Degrees. Most computer languages, including VB.Net work in Radians. We often need to adjust
Math.Sin, Math.Cos and Math.Tan so that they accept angles in Degrees. Angles can be converted using these functions
Private Function Radians_To_Degrees(radians As Double) As Double
Return radians * 180 / Math.PI
End Function
'
Private Function Degrees_To_Radians(degrees As Double) As Double
Return degrees * Math.PI / 180
End Function
Using these functions we can convert Sin, Cos and Tan
Dim Sin_Degrees As Double = Math.Sin(Radians_To_Degrees(angle))
Dim Cos_Degrees As Double = Math.Cos(Radians_To_Degrees(angle))
Dim Tan_Degrees As Double = Math.Tan(Radians_To_Degrees(angle))
The above code functions correctly, however, when processing complex formula, the VB code can become overly verbose or difficult to follow.
For this reasons, progammers often define separate functions for Sine_using_Degrees, Cosine_using_Degrees and Tan_using_Degrees.
Private Function Sin_Deg(degrees As Double) As Double
Return Math.Sin(degrees * Math.PI / 180)
End Function
'
Private Function Cos_Deg(degrees As Double) As Double
Return Math.Cos(degrees * Math.PI / 180)
End Function
'
Private Function Tan_Deg(degrees As Double) As Double
Return Math.Tan(degrees * Math.PI / 180)
End Function
ArcSine, ArcCosine and ArcTangent
Common variations of these function names include Inv_Sine Inv_Cosine, Inv_Tangent, Sine
-1
, Cos
-1
or Tan
-1
.
VB.Net abbreviates them to ASin, ACos and ATan.
Pocket calculators, astronomy books and formula lists often measure angles in Degrees. Most computer languages, including VB.Net work in Radians. We often need to adjust
Math.ASin, Math.ACos and Math.ATan so that they return the angle in Degrees. Again, one possible solution is to code our own functions
Private Function ASin_Deg(x As Double) As Double
Return Math.Asin(x) * 180 / Math.PI
End Function
'
Private Function ACos_Deg(x As Double) As Double
Return Math.Acos(x) * 180 / Math.PI
End Function
'
Private Function ATan_Deg(x As Double) As Double
Return Math.Atan(x) * 180 / Math.PI
End Function
ArcTangent2
Although converting ArcTangent2 from Radians to Degrees follows the same process as Asin, Acos or ATan, this will sometimes return the wrong result!
Most computer languages have a version of Atan2 which accepts two parameters. (in VB.Net we use = Math.Atan2(a,b))
Unfortunately, they do not agree which parameter goes first - is it =ATan(a,b) or =ATan(b,a).e.g. The excel function =ATAN2(1, 2) returns the same result as the VB.Net function =ATan2(2, 1)
If you get unexpected results, swapping the order or the Atan2 parameters may resolve the issue.
' Either use
Private Function ATan2_Deg(y As Double, x As Double) As Double
Return Math.Atan2(y, x) * 180 / Math.PI
End Function
' Or use
Private Function ATan2_Deg(x As Double, y As Double) As Double
Return Math.Atan2(y, x) * 180 / Math.PI
End Function
' decide the correct version using test values that give a known answer.
Other Trignoemtric Functions
The following functions handle some of the less well known trignometric functions. I have provided both Degrees and Radians variations for each Trig.
Public Function Sec(ByVal radians As Double) As Double
Return 1 / Math.Cos(radians)
End Function
'
Public Function Sec_Deg(ByVal degrees As Double) As Double
Return 1 / Math.Cos(degrees * Math.PI / 180)
End Function
'
Public Function Cosec(radians As Double) As Double
Return 1 / Math.Sin(radians)
End Function
'
Public Function Cosec_Deg(degrees As Double) As Double
Return 1 / Math.Sin(degrees * Math.PI / 180)
End Function
'
Public Function Cot(radians As Double) As Double
Return 1 / Math.Tan(radians)
End Function
'
Public Function Cot_Deg(degrees As Double) As Double
Return 1 / Math.Tan(degrees * Math.PI / 180)
End Function
Other Inverse Trignometric Functions
I have provided both Degrees and Radians variations for each Inverse Trig.
Public Function ASec(x As Double) As Double
Return Math.Acos(1 / x)
End Function
'
Public Function ASec_Deg(x As Double) As Double
Return Math.Acos(1 / x) * 180 / Math.PI
End Function
'
Public Function ACosec(x As Double) As Double
Return Math.Asin(1 / x)
End Function
'
Public Function ACosec_Deg(x As Double) As Double
Return Math.Asin(1 / x) * 180 / Math.PI
End Function
'
Public Function ACot(x As Double) As Double
Return Math.Atan(1 / x)
End Function
'
Public Function ACot_Deg(x As Double) As Double
Return Math.Atan(1 / x) * 180 / Math.PI
End Function
DigitalDan.co.uk