Results 1 to 16 of 16

Thread: [RESOLVED] Trigonometry… Not getting right results...

  1. #1

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

    Resolved [RESOLVED] Trigonometry… Not getting right results...

    I have this chart i'm testing my results against.
    Are these even correct values? Any pointers where i'm going wrong?

    Name:  487880178_1352309669203972_7403051815264587941_n.jpg
Views: 422
Size:  57.6 KB

    Code:
    Private Sub calculate()
        Select Case ComboBox1.SelectedIndex
            Case 0 ' sine(sin)
                Label3.Text = $"Math.Sin({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(sine(toRadians(NumericUpDown1.Value)))
            Case 1 ' cosine(cos)
                Label3.Text = $"Math.Cos({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(cosine(toRadians(NumericUpDown1.Value)))
            Case 2 ' tangent(tan)
                Label3.Text = $"Math.Tan({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(tangent(toRadians(NumericUpDown1.Value)))
            Case 3 ' cotangent(cot)
                Label3.Text = $"1 / Math.Tan({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(cotangent(toRadians(NumericUpDown1.Value)))
            Case 4 ' secant(sec)
                Label3.Text = $"1 / Math.Cos({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(secant(toRadians(NumericUpDown1.Value)))
            Case 5 ' cosecant(csc)
                Label3.Text = $"1 / Math.Sin({NumericUpDown1.Value} * Math.PI / 180)"
                Label4.Text = round(cosecant(toRadians(NumericUpDown1.Value)))
            Case Else ' -1
                Label3.Text = ""
                Label4.Text = ""
        End Select
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NumericUpDown1.Maximum = Decimal.MaxValue
        NumericUpDown2.Maximum = Integer.MaxValue
    End Sub
    
    Private Function sine(radians As Double) As Double
        Return Math.Sin(radians)
    End Function
    
    Private Function cosine(radians As Double) As Double
        Return Math.Cos(radians)
    End Function
    
    Private Function tangent(radians As Double) As Double
        Return Math.Tan(radians)
    End Function
    
    Private Function cotangent(radians As Double) As Double
        Dim t As Double = tangent(radians)
        Return 1 / t
    End Function
    
    Private Function secant(radians As Double) As Double
        Dim c As Double = cosine(radians)
        Return 1 / c
    End Function
    
    Private Function cosecant(radians As Double) As Double
        Dim s As Double = sine(radians)
        Return 1 / s
    End Function
    
    Private Function toRadians(d As Decimal) As Double
        Return d * Math.PI / 180
    End Function
    
    Private Function round(d As Double) As String
        If Double.IsNegativeInfinity(d) Or Double.IsPositiveInfinity(d) Then Return "Infinity"
        Return d.ToString
    End Function
    Last edited by .paul.; May 27th, 2025 at 05:05 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: Not getting right results...

    I cannot confirm if the values are correct or not because my math is... well I went to public school, lets just leave it at that.

    However, regarding the values not lining up in vb.net, my gut reaction is that there is a rounding error somewhere and it looks like you're mixing decimal/double in a few places.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

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

    Re: Not getting right results...

    There’s no explicit rounding. The value coming from the NumericUpDown is Decimal. All of the functions including toRadians return Doubles. Most of the functions including round, where I haven’t made any attempts at rounding yet, accept a Double. So it’s not a mishmash of datatypes…

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

    Re: Trigonometry… Not getting right results...

    Show your input and output values.

  5. #5

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

    Re: Trigonometry… Not getting right results...

    Quote Originally Posted by OptionBase1 View Post
    Show your input and output values.
    Name:  28-05-2025 00.32.00.png
Views: 372
Size:  8.0 KBName:  28-05-2025 00.32.47.png
Views: 363
Size:  8.1 KBName:  28-05-2025 00.33.33.png
Views: 367
Size:  8.4 KBName:  28-05-2025 00.35.58.png
Views: 361
Size:  7.9 KB

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

    Re: Trigonometry… Not getting right results...

    The values you are getting are reasonable. You get "practically zero", or "practically infinity" when the correct answers are either zero or infinity.

    The issue you are running in to is that Math.PI is only an approximation, of course, since PI is irrational and has an infinite number of digits.

    If you were to order the calculation as NUD.Value / 180 * Math.PI, it might actually make a difference in this case.

  7. #7

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

    Re: Trigonometry… Not getting right results...

    Ok thanks. I’ll try that shortly…

  8. #8

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

    Re: Trigonometry… Not getting right results...

    How can I detect ‘almost infinity’ and dismiss it as infinity?

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

    Re: Trigonometry… Not getting right results...

    Quote Originally Posted by .paul. View Post
    How can I detect ‘almost infinity’ and dismiss it as infinity?
    You don't. You need to pass your trig functions the angle in degrees and then, based on those degree values, you either return their calculated value (after converting to radians), or you handle infinity as you see fit. Once you convert the angle to radians, you are introducing inaccurate values at the nth decimal point, and that is why you are seeing the results you are.

    In csc, air code for example:

    Code:
    If (Angle MOD 180 = 0) Then
      'CSC(Angle) = Infinity, handle it as you see fit
    Else
      Return 1/Sin(ToRadians(Angle))
    End If
    Edit to add: Since you are using a NUD, I'm operating under the assumption that you are working with integer angle values in degrees.

  10. #10

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

    Re: Trigonometry… Not getting right results...

    I’m testing with simple numbers, just Integers at this time. I was expecting that once I can get consistent results with simple integers, decimals would also work.

    Edit: Yes the NUD contains degrees

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

    Re: Trigonometry… Not getting right results...

    If you want to allow any arbitrary value, then you would likely need to introduce some sort of sanity check function that effectively says (air code):

    Code:
    If Math.ABS(Result) < 0.00000000001 Then
      'Treat result as zero
    Else If Math.ABS(Result) > 100000000000
      'Treat result as infinity
    End If
    Of course, the bounds of what is "close enough" to 0 to be treated as 0, and the same for infinity, would be up to you to decide.

  12. #12

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

    Re: Trigonometry… Not getting right results...

    Quote Originally Posted by OptionBase1 View Post
    If you want to allow any arbitrary value, then you would likely need to introduce some sort of sanity check function that effectively says (air code):

    Code:
    If Math.ABS(Result) < 0.00000000001 Then
      'Treat result as zero
    Else If Math.ABS(Result) > 100000000000
      'Treat result as infinity
    End If
    Of course, the bounds of what is "close enough" to 0 to be treated as 0, and the same for infinity, would be up to you to decide.
    That's great. Using something along those lines, my functions agree with the values in my chart in my OP.
    Thanks again, that's just what i needed. Trigonometry isn't my strongest suit

  13. #13

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

    Re: [RESOLVED] Trigonometry… Not getting right results...

    It’s testing times like these that Wolfram Alpha is very useful.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [RESOLVED] Trigonometry… Not getting right results...

    You might also handle the edge cases differently. For example, Tan(A) where A = 90 is infinity, but it is not infinity where A = 90.0001.

    I have a program that makes use of some of those functions. The inputs are unpredictable. Therefore, I check for the specific cases that I don't allow, such as A = 90 in that preceding example (actually, I may allow that, but not the square root if an input is negative). Basically, I'm sanitizing my inputs, rather than dealing with the outputs.
    My usual boring signature: Nothing

  15. #15

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

    Re: [RESOLVED] Trigonometry… Not getting right results...

    Quote Originally Posted by Shaggy Hiker View Post
    You might also handle the edge cases differently. For example, Tan(A) where A = 90 is infinity, but it is not infinity where A = 90.0001.

    I have a program that makes use of some of those functions. The inputs are unpredictable. Therefore, I check for the specific cases that I don't allow, such as A = 90 in that preceding example (actually, I may allow that, but not the square root if an input is negative). Basically, I'm sanitizing my inputs, rather than dealing with the outputs.
    Using OB1's suggestion, i have this...

    Code:
    Private Function tangent(d As Decimal) As result
        Dim r As New result
        Dim v As Double = Math.Tan(toRadians(d))
        If Math.Abs(v) < 0.00000000001 Then
            r.value = 0
        ElseIf Math.Abs(v) > 100000000000 Then
            r.isInfinity = True
        Else
            r.value = v
        End If
        Return r
    End Function
    With an angle value of 90.0001

    Name:  28-05-2025 17.36.47.png
Views: 241
Size:  8.5 KB

    I hadn't started testing with Decimals yet, but the result from that is acceptable.

  16. #16

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

    Thumbs up Re: [RESOLVED] Trigonometry… Not getting right results...

    Since the last post, i've tried some other decimals...

    Name:  29-05-2025 20.27.46.png
Views: 243
Size:  8.4 KBName:  29-05-2025 20.28.20.png
Views: 232
Size:  8.7 KBName:  29-05-2025 20.28.58.png
Views: 226
Size:  8.7 KB

    If anyone is interested, here's the project. I'd appreciate any feedback...

    Trigonometry_Calculator.zip

    Edit: I know this should probably be in the codebank, but I posted here as it is relevant to this thread…
    Last edited by .paul.; May 29th, 2025 at 03:17 PM.

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