|
-
May 27th, 2025, 04:18 PM
#1
[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?

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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 04:49 PM
#2
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.
-
May 27th, 2025, 05:01 PM
#3
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 06:26 PM
#4
Re: Trigonometry… Not getting right results...
Show your input and output values.
-
May 27th, 2025, 06:37 PM
#5
Re: Trigonometry… Not getting right results...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 06:50 PM
#6
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.
-
May 27th, 2025, 06:59 PM
#7
Re: Trigonometry… Not getting right results...
Ok thanks. I’ll try that shortly…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 07:00 PM
#8
Re: Trigonometry… Not getting right results...
How can I detect ‘almost infinity’ and dismiss it as infinity?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 07:14 PM
#9
Re: Trigonometry… Not getting right results...
 Originally Posted by .paul.
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.
-
May 27th, 2025, 07:24 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 07:30 PM
#11
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.
-
May 27th, 2025, 09:47 PM
#12
Re: Trigonometry… Not getting right results...
 Originally Posted by OptionBase1
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2025, 11:09 PM
#13
Re: [RESOLVED] Trigonometry… Not getting right results...
It’s testing times like these that Wolfram Alpha is very useful.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 28th, 2025, 08:27 AM
#14
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
 
-
May 28th, 2025, 11:40 AM
#15
Re: [RESOLVED] Trigonometry… Not getting right results...
 Originally Posted by Shaggy Hiker
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

I hadn't started testing with Decimals yet, but the result from that is acceptable.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 29th, 2025, 02:34 PM
#16
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|