Hack Forums
[.NET] Trigonometric Function Calculator - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Trigonometric Function Calculator (/showthread.php?tid=4523105)



Trigonometric Function Calculator - Cynn - Nov 4, 2014

Alright everyone...I managed to run into a logic error while trying to make a basic Trigonometric calculator...I'm honestly confused and currious to why this is happening.
I'm fairly new to trig and thought I'd toy around with it a bit
(I know VB.Net Isn't the best language for math 😝)

Heres the code:
PHP Code

<?php 
Public Class Form1
    Dim x
, y, r, cos, cot, csc, sec, sin, tan As Double

    
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
      x 
= Double.Parse(txt_X_value.Text)
      
y = Double.Parse(txt_Y_value.Text)
      
r = Math.Sqrt((x ^ 2) + (y ^ 2))
      
cos = x / r
      cot 
= x / y
      csc 
= r / y
      sec 
= r / x
      sin 
= y / r
      tan 
= y / x

      lb_cos
.Text = cos.ToString
      lb_cot
.Text = cos.ToString
      lb_csc
.Text = csc.ToString
      lb_r
.Text = r.ToString
      lb_sec
.Text = sec.ToString
      lb_sin
.Text = sin.ToString
      lb_tan
.Text = sin.ToString

    End Sub

    
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click 'Clear Current Values Button
      txt_X_value.Text = "0"
      txt_Y_value.Text = "0"
      lb_r.Text = "0"
      lb_cos.Text = "0"
      lb_cot.Text = "0"
      lb_csc.Text = "0"
      lb_sec.Text = "0"
      lb_sin.Text = "0"
      lb_tan.Text = "0"
    End Sub
End Class 
and here's a screenshot:Spoiler (Click to View)
The error can be seen in the screenshot where the label says "tan θ = 0.110431526074847"
The tangent should by no means be 0.110431526074847, It should be 9.
If you have any advice (Other than "Don't do math in Vb.Net") I'd love to hear it.


* - Malware - Nov 4, 2014

Your problem is when you set the value equal to like x / y, it should be y / x.

Math error, not programming!

[Image: fc01f67d4d053cec45418b32ddc272a2.png]


* - Cynn - Nov 4, 2014

(Nov 4, 2014 10:24 PM)Venomous Net Wrote:  Your problem is when you set the value equal to like x / y, it should be y / x.

Math error, not programming!

[Image: fc01f67d4d053cec45418b32ddc272a2.png]

Oh thanks man...Damn. As I said Im new to trig 😖


Its still quite strange that when I'm using test data I'm comming up with and incorrect awnser (My test data is also being tested on my TI-83 Plus)
for example: tan θ should have equal 9


* - Malware - Nov 4, 2014

(Nov 4, 2014 10:34 PM)Cynic. Wrote:  Oh thanks man...Damn. As I said Im new to trig 😖


Its still quite strange that when I'm using test data I'm comming up with and incorrect awnser (My test data is also being tested on my TI-83 Plus)
for example: tan θ should have equal 9

Make sure you are in degrees and the function is correct!


* - Hawk HF - Nov 5, 2014

Meh.

Code
lb_tan.Text = sin.ToString

Could be this.


* - Cynn - Nov 5, 2014

(Nov 5, 2014 03:33 PM)Hawk HF Wrote:  Meh.

Code
lb_tan.Text = sin.ToString

Could be this.
Holy shit 😯 thanks...I honestly wouldnt have caught this...damn. I feel really dumb now 🤨


* - SaketSaket - Nov 6, 2014

@OP why you are individually setting each label's value to 0...

Try something like this...

PHP Code

<?php 
For Each ctl As Control In Me.Controls
If TypeOf ctl Is Label Then
Dim lbl 
As Label = DirectCast(ctl, Label)
lbl.Text = ""
End If
Next ctl 

Regards
Saket


* - Cynn - Nov 6, 2014

(Nov 6, 2014 12:16 AM)SaketSaket Wrote:  @OP why you are individually setting each label's value to 0...

Try something like this...

PHP Code

<?php 
For Each ctl As Control In Me.Controls
If TypeOf ctl Is Label Then
Dim lbl 
As Label = DirectCast(ctl, Label)
lbl.Text = ""
End If
Next ctl 

Regards
Saket
I didn't realize you could do something like that...that's actually really awesome o.o thanks man 🙂