Results 1 to 5 of 5

Thread: [RESOLVED] Calculating Trigonometrical formulas

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Resolved [RESOLVED] Calculating Trigonometrical formulas

    This is a small calculator i'm writing. I just need help with VB.Net formulas for cotangent, secant, and cosecant.
    Given an angle (NumericUpDown1.Value), how would i complete this code?

    Code:
    Private Sub calculate()
        Select Case ComboBox1.SelectedIndex
            Case 0 ' sine(sin)
                Label3.Text = $"Math.Sin({NumericUpDown1.Value})"
                Label4.Text = Math.Sin(NumericUpDown1.Value).ToString
            Case 1 ' cosine(cos)
                Label3.Text = $"Math.Cos({NumericUpDown1.Value})"
                Label4.Text = Math.Cos(NumericUpDown1.Value).ToString
            Case 2 ' tangent(tan)
                Label3.Text = $"Math.Tan({NumericUpDown1.Value})"
                Label4.Text = Math.Tan(NumericUpDown1.Value).ToString
            Case 3 ' cotangent(cot)
                Label3.Text = $"1 / Math.Tan({NumericUpDown1.Value})"
                Label4.Text = 1 / Math.Tan(NumericUpDown1.Value).ToString '???
            Case 4 ' secant(sec)
                Label3.Text = ""
                Label4.Text = "" '???
            Case 5 ' cosecant(csc)
                Label3.Text = ""
                Label4.Text = "" '???
            Case Else ' -1
                Label3.Text = ""
                Label4.Text = ""
        End Select
    End Sub
    I'm just testing this now. Should the angle be given in Radians?
    Last edited by .paul.; May 21st, 2025 at 10:08 AM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: Calculating Trigonometrical formulas

    Secant = 1/Cosine
    Cosecant = 1/Sin


    Of course, because both Sin and Cosine can equal 0 for theta=2n*pi and theta=2*(n+1)*pi, respectively, and where n is any integer, you need to code around "1/0" situations.

  3. #3
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,170

    Re: Calculating Trigonometrical formulas

    cotangent = cosine/sine (instead of tangent which is sine/cosine)
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Calculating Trigonometrical formulas

    Thanks to both of you. I'll do some testing before i mark this resolved...

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,747

    Re: Calculating Trigonometrical formulas

    And for working with angles in degrees instead of radians:
    Code:
    Dim angleRadians As Double = angleDegrees * Math.PI / 180
    Dim sinValue As Double = Math.Sin(angleRadians) 
    Dim cosValue As Double = Math.Cos(angleRadians)
    Dim tanValue As Double = Math.Tan(angleRadians)
    
    Dim cot As Double = 1 / tanValue 
    Dim sec As Double = 1 / cosValue 
    Dim csc As Double = 1 / sinValue

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width