|
-
Jun 4th, 2009, 03:11 AM
#1
Thread Starter
Member
Cos coming up with NaN
PHP Code:
BeforeInverse= ((2nd.Text ^2 + 3rd.Text ^ 2 - 1st.Text ^ 2) / (2 * 2nd.Text * 3rd.Text))
Calculation.Text = Math.Acos(BeforeInverse/ Math.PI * 180)
The above is using the cosine formula which has been rearranged to solve for the angle.
When i calculate, it comes up with NaN and i'm not dividing by 0. What does NaN mean and is there an error with the above code?
-
Jun 4th, 2009, 03:33 AM
#2
Re: Cos coming up with NaN
NaN means "Not a Number"
The first thing that leaps out from your code is you are doing mathematical operations on textboxes.
You should convert 2nd.text, 3rd.text into numbers - either singles, doubles or integers depending on what you expect.
-
Jun 4th, 2009, 03:40 AM
#3
Thread Starter
Member
Re: Cos coming up with NaN
So do i do that by going for example Dim 1st as Double = 1st.Text?
-
Jun 4th, 2009, 03:58 AM
#4
Re: Cos coming up with NaN
Yes, although you can't have a variable name starting with a number.
-
Jun 4th, 2009, 04:10 AM
#5
Thread Starter
Member
Re: Cos coming up with NaN
Okay thanks mate.
Another problem
I went: Dim First as Double = 1st.Text
And it came up saying some error about NullReferenceException was unhandeled. Then it said use New?
-
Jun 4th, 2009, 04:14 AM
#6
Re: Cos coming up with NaN
I'm confused as to how you can have a textbox named "1st"
This is just a variable like any other, and so should be subject to the same naming rules.
-
Jun 4th, 2009, 04:18 AM
#7
Thread Starter
Member
Re: Cos coming up with NaN
Sorry dude, i dunno why i typed the abbreviations did it quick. I retyped out the code shoulda just copy and pasted. Yeah they are all first and second and third, but there is still a problem with the way that above is working. NullUnhandledException for
Dim First as Double = First.Text
-
Jun 4th, 2009, 04:29 AM
#8
Re: Cos coming up with NaN
Actually sorry my mind is not working - haven't read your code properly, you need to parse the text value properly :
Code:
Dim First As Double
If Double.TryParse(First.Text, First) Then
'OK
Else
'can't convert
End If
And you need to put code in the If...Else to handle what happens if the content of the text box can't be converted.
Last edited by keystone_paul; Jun 4th, 2009 at 04:45 AM.
-
Jun 4th, 2009, 04:38 AM
#9
Thread Starter
Member
Re: Cos coming up with NaN
I just whipped it in and gave it a go, still NaN.
-
Jun 4th, 2009, 04:45 AM
#10
Re: Cos coming up with NaN
OK, so have you checked that all First, Second and Third contain valid numbers within the range that you are expecting?
For Acos to work, you need to pass in a value between -1 and +1 - so what does BeforeInverse evaluate to?
-
Jun 4th, 2009, 07:22 AM
#11
Re: Cos coming up with NaN
To expand on what Paul is saying, I'd create at least 3 intermediate variables, and variables of the correct data type for each text box.
Convert each text box to a number.
Store the calculated numerator in a variable.
Store the calculated denominator in a variable.
Perform the 'BeforeInverse' calculation.
Convert the BeforeInverse variable to another variable (BeforeInverse / Math * Pi).
Put that variable into your Inverse Cosine (which, as already stated, must be between +/-1).
Then, debug by stepping through: you will find your error within seconds.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 4th, 2009, 09:04 AM
#12
Re: Cos coming up with NaN
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
|