Results 1 to 12 of 12

Thread: Cos coming up with NaN

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2009
    Location
    Auckland, New Zealand
    Posts
    34

    Cos coming up with NaN

    PHP Code:
     BeforeInverse= ((2nd.Text ^3rd.Text 1st.Text 2) / (2nd.Text 3rd.Text))
                    
    Calculation.Text Math.Acos(BeforeInverseMath.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?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2009
    Location
    Auckland, New Zealand
    Posts
    34

    Re: Cos coming up with NaN

    So do i do that by going for example Dim 1st as Double = 1st.Text?

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cos coming up with NaN

    Yes, although you can't have a variable name starting with a number.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2009
    Location
    Auckland, New Zealand
    Posts
    34

    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?

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2009
    Location
    Auckland, New Zealand
    Posts
    34

    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

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2009
    Location
    Auckland, New Zealand
    Posts
    34

    Re: Cos coming up with NaN

    I just whipped it in and gave it a go, still NaN.

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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?

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Cos coming up with NaN

    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width