Nov 23

There was a question on a forum today: someone wanted to create a function in Visual Basic that allowed him to calculate the arcsine and arccosine functions from a given double. I found a nice solution, thought I’d share it here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Public Class arcCalc
 
    Public Function ArcCos(ByVal X As Double) As Double
        If X <> 1 Then
            ArcCos = Math.Atan(-X / Math.Sqrt(-X * X + 1)) + 2 * Math.Atan(1)
        Else
            ArcCos = 0
        End If
    End Function
 
    Public Function ArcSin(ByVal X As Double) As Double
        If X <> 1 Then
            ArcSin = Math.Atan(X / Math.Sqrt(-X * X + 1))
        Else
            ArcSin = 90
        End If
    End Function
 
    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
        Dim resultRC, resultDC, resultRS, resultDS As Double
        resultRC = ArcCos(txtBoxVal.Text)
        lblRadiusResultCos.Text = resultRC.ToString()
 
        resultDC = ((resultRC * 180) / Math.PI)
        lblDegreeResultCos.Text = resultDC.ToString()
 
        resultRS = ArcSin(txtBoxVal.Text)
        lblRadiusResultSin.Text = resultRS.ToString()
 
        resultDS = ((resultRS * 180) / Math.PI)
        lblDegreeResultSin.Text = resultDS.ToString()
 
    End Sub
End Class

Output from the running application - which received the name: Arc* Calculator :)

Arc* Calculator

Arc* Calculator