Question Debug problems (probably Math syntax)

Vermiculus

Well-known member
Joined
Jul 28, 2008
Messages
55
Location
Baltimore
Programming Experience
1-3
I have been over my code several times, but I just cant seem to find the error. It is supposed to be a simple triangle calculator (actually a control for a much bigger project) that solves for any missing parts.

This is the SSS (Side-Side-Side) portion of it that does not work...

VB.NET:
Public Function SolveTriangle(ByVal t As Int16)
        Dim AA, AB, AC, SA, SB, SC As Double
        AA = Double.TryParse(txtAA.Text, True)
        AB = Double.TryParse(txtAB.Text, True)
        AC = Double.TryParse(txtAC.Text, True)
        SA = Double.TryParse(txtSA.Text, True)
        SB = Double.TryParse(txtSB.Text, True)
        SC = Double.TryParse(txtSC.Text, True)
        'Check which scenario is appliccable; t is and identifier (binary: ABCabc)
        If t = 7 Then
            AA = Acos((SB ^ 2 + SC ^ 2 - SA ^ 2) / (2 * SB * SC) * PI / 180) * 180 / PI
            AB = Acos((SA ^ 2 + SC ^ 2 - SB ^ 2) / (2 * SA * SC) * PI / 180) * 180 / PI
            AC = Acos((SA ^ 2 + SB ^ 2 - SC ^ 2) / (2 * SA * SB) * PI / 180) * 180 / PI
            txtAA.Text = AA
            txtAB.Text = AB
            txtAC.Text = AC
            MsgBox(AC)
            MsgBox(AB)
            MsgBox(AA)
        End If
        Return Nothing
    End Function
 

Vermiculus

Well-known member
Joined
Jul 28, 2008
Messages
55
Location
Baltimore
Programming Experience
1-3
i have actually started tracing down the problem and it seems as though i have a problem in the actual setting of the Double...

I tried chenging the shabang to ConvertToDouble(string), then to Val(string) As Double; but neither worked.. any suggestions would be greatly appreciated

btw, the MsgBox(AA), etc are just debug techniques i used...they aren't really inportant to the function of the program
 

Vermiculus

Well-known member
Joined
Jul 28, 2008
Messages
55
Location
Baltimore
Programming Experience
1-3
Update

it seems as though the problem is in the If statement
Why not use a NumericUpDown control? It is a textbox used for numeric only input, the Value property is Decimal type.

Ah, but it would take a heck of along time to get up there...unless you could type in it....hmmmm...*fiddles* oooo ahhhhh oooo ahhhhh ..... ..... ..... ..... this is a-maz-ing

THANKS!!!

Ok, I have tried to convert the NumericUpDown.Value to a Double, but I've tried all I know...the Convert module, CType, and NumericUpDown.Value.ToDouble. any suggestions? thanks-

and if there is any way to make it a double by default, that would be cool too
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,468
Location
Norway
Programming Experience
10+
VB.NET:
Dim dec As Decimal, Dim dbl As Double

dbl = dec
dbl = Decimal.ToDouble(dec)
dbl = CDbl(dec)
dbl = CType(dec, Double)
dbl = Convert.ToDouble(dec)
Note the first option where value is assigned directly, this is an implicit widening CType conversion allowed by option Strict and one that will not cause overflow.
 

Vermiculus

Well-known member
Joined
Jul 28, 2008
Messages
55
Location
Baltimore
Programming Experience
1-3
Thanks, ...

Well, I now have yet another problem...You know how 'all angles of a triangle add to 180 degrees'? well mine don't:(. now this must really be in the math, like I originally thought...
VB.NET:
Public Class TriangleSolver

    Private Sub btnSolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolve.Click
        'Declare all my variables
        Dim AA, AB, AC, SA, SB, SC, H As Double
        '
        'Try to convert my NumUpDowns to Doubles
        '
        AA = CDbl(numAA.Value)
        AB = CDbl(numAB.Value)
        AC = CDbl(numAC.Value)
        SA = CDbl(numSA.Value)
        SB = CDbl(numSB.Value)
        SC = CDbl(numSC.Value)
        H = CDbl(numH.Value)
        'Determine what laws I use to solve the triangle using a binary method
        Dim WhatVersion As Int16 = 0
        If numH.Value > 0.001 Then
            WhatVersion += 64
        End If
        If numAA.Value > 0.001 Then
            WhatVersion += 32
        End If
        If numAB.Value > 0.001 Then
            WhatVersion += 16
        End If
        If numAC.Value > 0.001 Then
            WhatVersion += 8
        End If
        If numSA.Value > 0.001 Then
            WhatVersion += 4
        End If
        If numSB.Value > 0.001 Then
            WhatVersion += 2
        End If
        If numSC.Value > 0.001 Then
            WhatVersion += 1
        End If
        '''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''------------Solve-------------''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''
        If WhatVersion = 7 Then
            'I use the Law of Cosines to figure the angles
            AA = ((Math.Acos((SB ^ 2 + SC ^ 2 - SA ^ 2) / (2 * SB * SC))) * 180) / Math.PI
            AB = ((Math.Acos((SA ^ 2 + SC ^ 2 - SB ^ 2) / (2 * SA * SC))) * 180) / Math.PI
            AC = ((Math.Acos((SA ^ 2 + SB ^ 2 - SC ^ 2) / (2 * SA * SC))) * 180) / Math.PI
            numAA.Value = AA
            numAB.Value = AB
            numAC.Value = AC
            'Here is where I test the angles I get _
            'by adding them up, making sure the sum is 180
            MsgBox(AA + AB + AC)
        End If
    End Sub
End Class

 
Last edited:

egypt4egypt90

New member
Joined
Jul 21, 2009
Messages
2
Programming Experience
10+
Well, I now have yet another problem...You know how 'all angles of a triangle add to 180 degrees'? well mine don't:(. now this must really be in the math, like I originally thought...
VB.NET:
Public Class TriangleSolver

    Private Sub btnSolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolve.Click
        'Declare all my variables
        Dim AA, AB, AC, SA, SB, SC, H As Double
        '
        'Try to convert my NumUpDowns to Doubles
        '
        AA = CDbl(numAA.Value)
        AB = CDbl(numAB.Value)
        AC = CDbl(numAC.Value)
        SA = CDbl(numSA.Value)
        SB = CDbl(numSB.Value)
        SC = CDbl(numSC.Value)
        H = CDbl(numH.Value)
        'Determine what laws I use to solve the triangle using a binary method
        Dim WhatVersion As Int16 = 0
        If numH.Value > 0.001 Then
            WhatVersion += 64
        End If
        If numAA.Value > 0.001 Then
            WhatVersion += 32
        End If
        If numAB.Value > 0.001 Then
            WhatVersion += 16
        End If
        If numAC.Value > 0.001 Then
            WhatVersion += 8
        End If
        If numSA.Value > 0.001 Then
            WhatVersion += 4
        End If
        If numSB.Value > 0.001 Then
            WhatVersion += 2
        End If
        If numSC.Value > 0.001 Then
            WhatVersion += 1
        End If
        '''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''------------Solve-------------''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''
        If WhatVersion = 7 Then
            'I use the Law of Cosines to figure the angles
            AA = ((Math.Acos((SB ^ 2 + SC ^ 2 - SA ^ 2) / (2 * SB * SC))) * 180) / Math.PI
            AB = ((Math.Acos((SA ^ 2 + SC ^ 2 - SB ^ 2) / (2 * SA * SC))) * 180) / Math.PI
            AC = ((Math.Acos((SA ^ 2 + SB ^ 2 - SC ^ 2) / (2 * SA * SC))) * 180) / Math.PI
            numAA.Value = AA
            numAB.Value = AB
            numAC.Value = AC
            'Here is where I test the angles I get _
            'by adding them up, making sure the sum is 180
            MsgBox(AA + AB + AC)
        End If
    End Sub
End Class



hello
pleas my dear
i want sorce code this code
(Project)
i use vb,net

thank you very much
your freind
mohamed fayed
 
Top Bottom