Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Trigonometric Function Calculator

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.
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]
(Nov 4, 2014 10:24 PM)Malware 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
(Nov 4, 2014 10:34 PM)Cynn 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!
Meh.

Code
lb_tan.Text = sin.ToString

Could be this.
(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 🤨
@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
BTC - bc1q3efew3yzfwhdz290dafykaw0a2w56g2j69g2c6
ETH - 0xf1c873217032468990D3313da6509D1b813c4671
USDT - 0xf1c873217032468990D3313da6509D1b813c4671
PP - [email protected]
(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 🙂


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FhackMar 2, 2022 03:52 AM 4 306 Mar 2, 2022 03:52 AM
Last Post: Fhack
[.NET] by AngularJSOct 26, 2014 08:15 AM 17 252 Oct 26, 2014 08:15 AM
Last Post: Cornflower Blue இڿڰۣ-ڰۣ—
[Python] by success = trueSep 14, 2013 07:05 PM 3 123 Sep 14, 2013 07:05 PM
Last Post: Dmaster_773