Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, July 25, 2011 5:40 AM
Im currently working on a program to solve quadratic formulas but I am caught at square rooting a decimal.
{
decimal a, b, c;
a = decimal.Parse(textBox15.Text);
b = decimal.Parse(textBox16.Text);
c = decimal.Parse(textBox17.Text);
decimal step1 = 4 * a;
decimal step2 = step1 * c;
decimal step3 = b * b - step2;
decimal step4 = 2 * a;
decimal step5 = Math.Sqrt(step3);
decimal step6 = step5 / step4;
decimal final1 = step6 + -b;
decimal final2 = step6 - -b;
textBlock36.Text = "x = " + final1;
textBlock37.Text = "x = " + final2;
}
How can I go about getting a square root from a decimal?
All replies (20)
Monday, July 25, 2011 8:12 AM âś…Answered
By the way catching many exceptions is a bad habit if you can really avoid it. :)
NaN will result if you are having an invalid equations.
Do an if statements where step3 is not a negative stuff, step4 is not a zero, final1, final2 and step 6 are not negative.
Maybe i'm wrong, it could be but try to analyze your code. :)
It's a good practice,
Eric Failure is not the worst thing in the world. The very worst is not to try. Email Address : [email protected]. http://ericjohnadamos.blogspot.com/
Monday, July 25, 2011 5:57 AM
I think there is no way to get decimal as a parameter in square root.
The reason for this is decimal is used when you are dealing with numbers that are exact.
Math class are using primitive types and CLR(Common Language Runtime) doesn't consider as primitive type.
You can use double as an alternative to this.
Eric
Failure is not the worst thing in the world. The very worst is not to try. Email Address : [email protected]. http://ericjohnadamos.blogspot.com/
Monday, July 25, 2011 5:58 AM | 2 votes
Hello aarhus, Check out below statement.
decimal step5 = (decimal)Math.Sqrt((double)step3);
Hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
Monday, July 25, 2011 6:05 AM
@Adavesh
That seems close but now its throwing me an overflowException error at that new line of code. "try not dividing by zero" which confuses me because im using try/catch for dividebyzeroexception
Monday, July 25, 2011 6:09 AM
Hello aarrhus,
@Adavesh
That seems close but now its throwing me an overflowException error at that new line of code. "try not dividing by zero" which confuses me because im using try/catch for dividebyzeroexception
math class only supports the method sqrt with numbers double to a decimal number must be created.
I advise you if you do not need to use the numbers as precise double and close all code in try / catch to handle any runtime exception any of Parse.
try
{
double a, b, c;
a = double.Parse(textBox15.Text);
b = double.Parse(textBox16.Text);
c = double.Parse(textBox17.Text);
double step1 = 4 * a;
double step2 = step1 * c;
double step3 = b * b - step2;
double step4 = 2 * a;
double step5 = Math.Sqrt(step3);
double step6 = step5 / step4;
double final1 = step6 + -b;
double final2 = step6 - -b;
textBlock36.Text = "x = " + final1;
textBlock37.Text = "x = " + final2;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(());
}
Bye
http://community.visual-basic.it/carmelolamonica/
Monday, July 25, 2011 6:11 AM
just add a try catch to it
try
{
// code here
}
catch (DividedByZeroException ex)
{
} Failure is not the worst thing in the world. The very worst is not to try. Email Address : [email protected]. http://ericjohnadamos.blogspot.com/
Monday, July 25, 2011 6:29 AM
Hello,
Call the below function instead of Math.Sqrt().
public decimal SquareRoot(decimal number)
{
decimal sqrt = 0;
decimal tempNumber1,tempNumber2;
decimal e = 0.00000000000000000001m;
sqrt = number;
tempNumber2 = sqrt * sqrt;
while (tempNumber2 - number >= e)
{
tempNumber1 = (sqrt + (number / sqrt)) / 2;
sqrt = tempNumber1;
tempNumber2 = sqrt * sqrt;
}
return sqrt;
}
Hope this helps you.
Please mark this post as answer if it solved your problem. Happy Programming!
Monday, July 25, 2011 6:30 AM
Im already using a try catch for dividedbyzero, @Adavesh When I try that my textBlocks give me NaN
Monday, July 25, 2011 6:33 AM
When you do what? When you call my SquareRoot function?Please mark this post as answer if it solved your problem. Happy Programming!
Monday, July 25, 2011 6:38 AM
When I switch decimal to double for everything.
Monday, July 25, 2011 6:52 AM
This is what I currently have but it is giving me a overflow exception when the button is pressed inside the emulator.
private void button6_Click(object sender, RoutedEventArgs e)
{
try
{
decimal a, b, c;
a = decimal.Parse(textBox15.Text);
b = decimal.Parse(textBox16.Text);
c = decimal.Parse(textBox17.Text);
decimal step1 = 4 * a;
decimal step2 = step1 * c;
decimal step3 = b * b - step2;
decimal step4 = 2 * a;
decimal step5 = (decimal)Math.Sqrt((double)step3);
decimal step6 = step5 / step4;
decimal final1 = step6 + -b;
decimal final2 = step6 - -b;
textBlock36.Text = "x = " + final1;
textBlock37.Text = "x = " + final2;
}
catch (DivideByZeroException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
catch (FormatException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
}
Monday, July 25, 2011 7:04 AM
Hello aahrus,
This is what I currently have but it is giving me a overflow exception when the button is pressed inside the emulator.
private void button6_Click(object sender, RoutedEventArgs e) { try { decimal a, b, c; a = decimal.Parse(textBox15.Text); b = decimal.Parse(textBox16.Text); c = decimal.Parse(textBox17.Text); decimal step1 = 4 * a; decimal step2 = step1 * c; decimal step3 = b * b - step2; decimal step4 = 2 * a; decimal step5 = (decimal)Math.Sqrt((double)step3); decimal step6 = step5 / step4; decimal final1 = step6 + -b; decimal final2 = step6 - -b; textBlock36.Text = "x = " + final1; textBlock37.Text = "x = " + final2; } catch (DivideByZeroException) { textBlock36.Text = "no input"; textBlock37.Text = "no input"; } catch (FormatException) { textBlock36.Text = "no input"; textBlock37.Text = "no input"; } }
Owerflow means that when calculating the square root whirlwind exceed the value allowed for a decimal number, you can also manage OverflowException, see example below.
private void button6_Click(object sender, EventArgs e)
{
try
{
decimal a, b, c;
a = decimal.Parse(textBox15.Text);
b = decimal.Parse(textBox16.Text);
c = decimal.Parse(textBox17.Text);
decimal step1 = 4 * a;
decimal step2 = step1 * c;
decimal step3 = b * b - step2;
decimal step4 = 2 * a;
decimal step5 = (decimal)Math.Sqrt((double)step3);
decimal step6 = step5 / step4;
decimal final1 = step6 + -b;
decimal final2 = step6 - -b;
textBlock36.Text = "x = " + final1;
textBlock37.Text = "x = " + final2;
}
catch (DivideByZeroException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
catch (FormatException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
catch (OverflowException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
}
Bye
http://community.visual-basic.it/carmelolamonica/
Monday, July 25, 2011 7:07 AM
Btw, note that NaN is equal to nothing
try
{
double a = 0.00, b = 0.00, c = 0.00;
a = double.Parse(textBox1.Text);
b = double.Parse(textBox2.Text);
c = double.Parse(textBox3.Text);
double step1 = 4 * a;
double step2 = step1 * c;
double step3 = b * b - step2;
double step4 = 2 * a;
double step5 = Math.Sqrt(step3);
double step6 = step5 / step4;
MessageBox.Show(Convert.ToString(step6));
double final1 = step6 + (-b);
double final2 = step6 - (-b);
MessageBox.Show(Convert.ToString(final1) + " " + Convert.ToString(final2));
}
catch (DivideByZeroException)
{
//textBlock36.Text = "no input";
//textBlock37.Text = "no input";
}
catch (FormatException)
{
//textBlock36.Text = "no input";
//textBlock37.Text = "no input";
}
Eric
Failure is not the worst thing in the world. The very worst is not to try. Email Address : [email protected]. http://ericjohnadamos.blogspot.com/
Monday, July 25, 2011 7:10 AM
That results in haveing no input for almost every variable, I need it to be a little more flexible than that.
Monday, July 25, 2011 7:13 AM
Did you try my SquareRoot function?Please mark this post as answer if it solved your problem. Happy Programming!
Monday, July 25, 2011 7:33 AM
@Eric Well I tried what you just suggested and it still results in equal to nothing.
Monday, July 25, 2011 7:49 AM
Hi aarhus,
Since you are working with quadratic equation, of course NAN will be the result if b^2 < 4ac, because the square root of negative number is error or infinite.
Hardz
Monday, July 25, 2011 7:53 AM
@Adavesh, I didnt really understand how I could implement that into my code. Would you mind show me how I could plug it into
private void button6_Click(object sender, RoutedEventArgs e)
{
try
{
decimal a, b, c;
a = decimal.Parse(textBox15.Text);
b = decimal.Parse(textBox16.Text);
c = decimal.Parse(textBox17.Text);
decimal step1 = 4 * a;
decimal step2 = step1 * c;
decimal step3 = b * b - step2;
decimal step4 = 2 * a;
decimal step5 = (decimal)Math.Sqrt((double)step3);
decimal step6 = step5 / step4;
decimal final1 = step6 + -b;
decimal final2 = step6 - -b;
textBlock36.Text = "x = " + final1;
textBlock37.Text = "x = " + final2;
}
catch (DivideByZeroException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
catch (FormatException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
catch (OverflowException)
{
textBlock36.Text = "no input";
textBlock37.Text = "no input";
}
:
Monday, July 25, 2011 8:28 AM
Thanks for the help in both situations I had tonight Eric, thank you everyone else as well. I will just have to settle with Not possible for negative results. Better than nothing.
Monday, July 25, 2011 9:22 AM
No problem again aarhus. :)
have a good day
Eric Failure is not the worst thing in the world. The very worst is not to try. Email Address : [email protected]. http://ericjohnadamos.blogspot.com/