Click to See Complete Forum and Search --> : Problem With Square Root and Quadratic Formula
Jayj2k1
Oct 10th, 2005, 12:42 PM
I basically set up my program as is and it does not run correctly. Right now I am recieiving a "Run Time Error '13': Type mismatch". I don't have the MSDN library because I didn't install it when I first bought the program. If any of you could give some input, that would be great. I have made a few alternations but here is the way I currently have it set up.
Dim X1 As String
Dim X2 As String
Dim a As String
Dim b As String
Dim c As String
Private Sub cmdCalculate_Click()
txtValueA = a
txtValueB = b
txtValueC = c
If X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
lblFirst.Caption = X1
End If
If X2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
lblSecond.Caption = X2
End If
End Sub
opus
Oct 10th, 2005, 01:38 PM
First of all, why are you using STRINGS to calculate? Use a numeric type!
Dim X1 As String
Dim X2 As String
Dim a As String
Dim b As String
Dim c As String
and furthermore,
what are you calculating??
Private Sub cmdCalculate_Click()
txtValueA = a 'where do you define a??
txtValueB = b 'where do you define b??
txtValueC = c 'where do you define c??
'What is X1? You only use it if it fits the formula?
If X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
lblFirst.Caption = X1
End If
'What is X2? You only use it if it fits the formula?
If X2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
lblSecond.Caption = X2
End If
End Sub
Jayj2k1
Oct 10th, 2005, 01:51 PM
Forget the String type. I'm not sure why I did that but instead I am now using double. Basically, I have set up a program that performs that allows a quadratic equation (Ex. Ax2 + Bx + C= 0), to be calculated with the quadratic formula. I made an attachment so that you can see the formula that I am trying to put into Visual Basic Code.
In my program, I created 3 text boxes where the end user will enter a value for A, B, and C (all numeric). Once they do this, they will click the Calculate command button where the program will use the formula. I have been getting a "Run Time Error '6': Overflow" as well. And by the way, how do I define A, B, and C. Once you look at the formula that I attached with the post, you should have a better idea of what I am trying to accomplish here. Thanks for your help and patience.
zaza
Oct 10th, 2005, 01:58 PM
Er..when you set A=B, this means that you are setting A to be equal to what is already in B.
Hence you need your definitions of a,b,c to be the other way around.
Also, you don't need "If...Then", you aren't checking a condition. You just need to set X1=... X2=...
Then stick them in the labels.
zaza
Jayj2k1
Oct 10th, 2005, 02:02 PM
So if you were to use three text boxes for the end user to enter A, B, and C and then use that formula that I have a picture of, how exactly would you code it to make it work all together?
zaza
Oct 10th, 2005, 02:28 PM
Hence you need your definitions of a,b,c to be the other way around.
Also, you don't need "If...Then", you aren't checking a condition. You just need to set X1=... X2=...
Then stick them in the labels.
zaza
Jayj2k1
Oct 10th, 2005, 03:35 PM
Here is what my program looks like now. Basically you are telling me to just enter in the value of X1 and X2 and I'm fine. However, what is the proper way to declare these values because X1 and X2 will be displayed in the two label boxes next to the X?
opus
Oct 10th, 2005, 03:43 PM
To display a numeric value in a textbox use:
Textbox.text=cstr(NumericValue)
To calculate your X1
a=csng(textboxA.text)
b=csng(textboxB.text)
c=csng(textboxC.text)
X1=(-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a
You should be able to do the rest!!
Jayj2k1
Oct 10th, 2005, 04:38 PM
Ok. The new code that I have in now runs through ok. I get errors at times depending on the numbers I put in for A, B, and C.
One thing I did notice is that my answers are way off. When I substituted 5 for A, 8 for B, and 2 for C, I came up with -7.75 for X1 and like -32.37 for X2.
I know that those answers are way off because normally your answer should be a difference of maybe positive 1 or 2. Nothing more than that. Once again, I will post my code as well as how the program looked when I ran it.
Option Explicit
Dim X1 As Double
Dim X2 As Double
Dim a As Double
Dim b As Double
Dim c As Double
Private Sub cmdCalculate_Click()
a = CSng(txtValueA.Text)
b = CSng(txtValueB.Text)
c = CSng(txtValueC.Text)
X1 = (-b + Sqr(b ^ 2 - (4 * a * c))) / 2 * a
X2 = (-b - Sqr(b ^ 2 - (4 * a * c))) / 2 * a
txtFirst.Text = CStr(X1)
txtSecond.Text = CStr(X2)
End Sub
Private Sub cmdClear_Click()
txtValueA = ""
txtValueB = ""
txtValueC = ""
txtFirst = ""
txtSecond = ""
End Sub
opus
Oct 11th, 2005, 09:35 AM
Your original formula had a mistake, use
X1 = (-b + Sqr(b ^ 2 - (4 * a * c))) / (2 * a)
X2 = (-b - Sqr(b ^ 2 - (4 * a * c))) /( 2 * a)
instead!
meboz
Oct 12th, 2005, 05:42 PM
have you considered the case of a = 0?
or when the solutions dont exist in R? eg the discriminant < 0?
jemidiah
Oct 13th, 2005, 12:02 AM
And what if the user enters text in the boxes instead of numbers?
That's where the nice if...then statements come in handy.
The IsNumeric(number) function returns true if the 'number' is really a number or false if not.
The other special cases, when b^2-4ac is less than or maybe equal to zero and when a = 0, can be handled using simple if statements. More questions would likely be handled best in the regular programming forum from here since the math part has been answered/dealt with. Of course, more discussion on the nature of the quadratic formula would qualify here :)
Edit: PS, nice UI
meboz
Oct 13th, 2005, 09:59 PM
isnumeric will do the job but is a legacy function, correct?
im curious to know what the best way of validating a text box for numeric data.
currently i would use a regex, but is there a cleaner way to do it?
dglienna
Oct 13th, 2005, 10:07 PM
You can use val(text1.text), but it will evaluate as 0 if there isn't a number.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.