1 Attachment(s)
[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?
Attachment 194889
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
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.
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…
Re: Trigonometry… Not getting right results...
Show your input and output values.
4 Attachment(s)
Re: Trigonometry… Not getting right results...
Quote:
Originally Posted by
OptionBase1
Show your input and output values.
Attachment 194890Attachment 194891Attachment 194892Attachment 194893
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.
Re: Trigonometry… Not getting right results...
Ok thanks. I’ll try that shortly…
Re: Trigonometry… Not getting right results...
How can I detect ‘almost infinity’ and dismiss it as infinity?
Re: Trigonometry… Not getting right results...
Quote:
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.
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
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.
Re: Trigonometry… Not getting right results...
Quote:
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 :D
Re: [RESOLVED] Trigonometry… Not getting right results...
It’s testing times like these that Wolfram Alpha is very useful.
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.
1 Attachment(s)
Re: [RESOLVED] Trigonometry… Not getting right results...
Quote:
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
Attachment 194902
I hadn't started testing with Decimals yet, but the result from that is acceptable.
4 Attachment(s)
Re: [RESOLVED] Trigonometry… Not getting right results...
Since the last post, i've tried some other decimals...
Attachment 194908Attachment 194909Attachment 194910
If anyone is interested, here's the project. I'd appreciate any feedback...
Attachment 194911
Edit: I know this should probably be in the codebank, but I posted here as it is relevant to this thread…