VB.net problem

Aastii

VIP Member
I am making a calculator in VB 2008 and trying to put in trig functions.

Now I know that when you do any trig function in VB it comes out in radians rather than degrees, and to convert from radians to degrees you must *Pi/180, however whenever I do it, it comes out wrong.

My code is:

Private Sub cmdSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSine.Click
FirstNumber = txtFirstNumber.Text
Answer = (Math.Sin(FirstNumber)) * (Math.PI / 180)
txtAnswer.Text = Answer

End Sub

Which should come out with the right answer, so to test it I did Sin 90 (or tried to) which should come out with 1 exactly, but it came out as 0.01560.....

Is it a mistake in VB, or am I missing something here?
 
Hmm.

What types are FirstNumber and Answer anyways? They should both be explicitly declared as either doubles or floats, I've seen funky things happen with decimal numbers when there's a couple of integers. I haven't programmed much with .NET languages, so I'm not sure, though...

Anyways, explicitly declaring variables is a good programming practice, even if that's not the problem you should do it.
 
Hmm.

What types are FirstNumber and Answer anyways? They should both be explicitly declared as either doubles or floats, I've seen funky things happen with decimal numbers when there's a couple of integers. I haven't programmed much with .NET languages, so I'm not sure, though...

Anyways, explicitly declaring variables is a good programming practice, even if that's not the problem you should do it.

First Number and answer are both double

Got them as:

Dim FirstNumber As Double = 0
Dim SecondNumber As Double = 0 (this isn't used in this particular one, but is for multiplying, asdding etc)
Dim Answer As Double = 0

So all should be alright there because they aren't decimal or integer :confused:

oh, I asked about whether it is 180/pi or pi/180 to my maths tutor today, he says it is 180/pi and showed me why and that, so turns out you were right :)
 
Back
Top