PDA

View Full Version : Trouble with quadratic formula

youngbuck
02-11-04, 06:39 PM
I'm about 3 weeks into a C++ class at high school and I'm stuck on this assignment.

The user is asked to enter 3 different numbers which are assigned to variables a, b, c, and the result of those numbers comes from the quadratic formula (only -b +..., leaving out -b - ...).

result = -b + sqrt(pow(b, 2) - 4*a*c)/ 2*a;

Thats all what I can come up with, and it's just not working out. I'll enter some numbers and the result is different than when I work it out on paper, and I know I'm not making a mistake on paper. Also, sometimes the result comes out to be: -1.#IND.

This assignment is due tomorrow...

-YB

02-11-04, 06:49 PM
Funny, I was just looking at Quadratic Formula today (while the rest of my class was working on graphing inequalities hehe!) While I was using my scientific calculator to figure out some formulas instead of doing it by hand, I discovered I had to put EACH step in parenthesis (i.e. to keep it from dividing the "top" by 2 and then multiplying it by a I had to put 2*a in parenthesis). Remember, C knows the order of operations!

deRusett
02-11-04, 06:49 PM
Originally posted by youngbuck

result = -b + sqrt(pow(b, 2) - 4*a*c)/ 2*a;

-YB

this is not entirely correct

you would need both
result = (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a;
result = (-b - sqrt(pow(b, 2) - 4*a*c))/ 2*a;

to cover just the formula its self

02-11-04, 07:16 PM
I've been looking at this and discovered the sqrt() function in MinGW (GCC 3.2) doesn't seem to work right. I always get 1.#IND00 returned from it.

youngbuck
02-11-04, 08:12 PM
Originally posted by deRusett

this is not entirely correct

you would need both
result = (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a;
result = (-b - sqrt(pow(b, 2) - 4*a*c))/ 2*a;

to cover just the formula its self

We only need to do the first one: result = (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a;,(I tried to say that in my original post... didn't do so very well). But any idea why it's not working? I have messed around with the parenthesis as much as possible, and I still get the wrong answer.

I'm using Microsoft Visual C++ 6.0 Standard Edition.

And what does -1.#IND mean?

-YB

Edit:

Another problem I see is that if I assign a to 1, b to 6, and c to 1, using the equation (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a; I get -0.171573 (which I know is the wrong answer). But if I take the first and last parenthesis off, leaving the equation as -b + sqrt(pow(b, 2) - 4*a*c)/ 2*a; the answer comes out to be -3.171573 (which is still wrong). I don't see how the order operations is changed from the first one to the second one to the point of determining a different result.

Roof Jumper
02-11-04, 08:25 PM
well, with those parentheses taken off the order of operations becoms:

-b + (sqrt(pow(b,2) - 4*a*c)/2*a)

I would recommend not using pow, its better for a square function just to have " ... (b * b)..."

deRusett
02-11-04, 11:17 PM
Originally posted by youngbuck

We only need to do the first one: result = (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a;,(I tried to say that in my original post... didn't do so very well). But any idea why it's not working? I have messed around with the parenthesis as much as possible, and I still get the wrong answer.

I'm using Microsoft Visual C++ 6.0 Standard Edition.

And what does -1.#IND mean?

-YB

Edit:

Another problem I see is that if I assign a to 1, b to 6, and c to 1, using the equation (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a; I get -0.171573 (which I know is the wrong answer). But if I take the first and last parenthesis off, leaving the equation as -b + sqrt(pow(b, 2) - 4*a*c)/ 2*a; the answer comes out to be -3.171573 (which is still wrong). I don't see how the order operations is changed from the first one to the second one to the point of determining a different result.

(-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a
and
-b + sqrt(pow(b, 2) - 4*a*c)/ 2*a

are not the same equation

in eq1 everything is over 2*a
in eq2 only sqrt(pow(b, 2) - 4*a*c) is over 2*a

I am assuming you have tried

(-b + sqrt(pow(b, 2) - 4*a*c))/ (2*a)

also have you tried making these a variables

d=pow(b, 2)
e=4*a*c

so
(-b + sqrt(d - e))/ (2*a)

so your main equation has less stuff to deal with, kinda adds bloat to your code BUT if it works thats more important

youngbuck
02-11-04, 11:23 PM
I'm working on another project now, and something some more crap is going on. I'm honestly beginning to think that there's something wrong with my compiler (was aquired off that place where you download stuff for \$0.00.) There are many reasons for me to believe this. I'll try it all at my compiler at school tomorrow, hopefully have better luck there. Teacher will be there too.

deRusett
02-12-04, 12:19 AM
http://www.bloodshed.net/compilers/index.html

here get your self a good FREE compiler, that works for sure

02-12-04, 11:55 AM
I'm using the Bloodshed DevC++ developer environment (with the MinGW compiler) and as I noted above, I can't seem to get the sqrt function to work. Here is some code that I think will find the square root of a number to 8 decimal points.

double rootfinder(double input)
{ if (input < 0) input *= -1; // you can't find the square root of a negative number
double result;
double tento = 0;
while (result < input)
{ tento ++;
result = pow(10, tento);
}
tento--;
result = 0;
result = pow(10, tento);
double times;
while (tento > -9)
{ for (times = 1; times < 10 || pow(result, 2) > input; times++)
{ result -= ((times--) * pow(10, tento));
result += (times * pow(10, tento));
}
if (result > input)
{ result -= (times * pow(10, tento));
result += ((times - 1) * pow(10, tento));
}
tento--;
}
return result;
}

I don't have a compiler with me to check this code so it may not work.

JigPu
02-12-04, 01:07 PM
Instead of using sqrt(x); have you tried using pow(x,0.5); ? They're the same operation, and if the first dosen't work for you, the second may just.

EDIT: Here's how it would look... (I took out the pow(b, 2) cuz it wastes space compared to b*b... :))

Original :: (-b + sqrt(pow(b, 2) - 4*a*c))/ 2*a
Revised :: (-b + pow((b*b - 4*a*c), 0.5)) / 2*a;

JigPu

Titan386
02-12-04, 01:55 PM
Have you remembered to include math.h? You should have the following line near the top of your source file:
#include <math.h>

If you do not have this, many math functions (such as pow() and sqrt()) will not be defined via their prototypes in the header files, but will instead be implicitly defined as you use them. This is bad because it disables the compiler's type checking, and assumes the function returns an int. If this is not the case, such as for pow() or sqrt(), which return doubles, the result obtained will be garbage.

gcc has an option to enable warnings for implicit declarations; enable it. It will tell you if you have failed to properly declare any functions before you use them. (IMO, its good practice to enable all compiler warnings: they will catch potential problems that are very hard for a human to find).

youngbuck
02-12-04, 02:01 PM
If a=1, b=6, and c=1, what do you get as an the result when using the equation above?

What about a=6, b=3, and c=3? I get -1.#IND. And btw, what does that mean?

-YB

P.S., yes I do have the <cmath.h> header listed. For some reason it won't work on my compiler. It says the file can't be found. But if I put #include <cmath> w/out the .h it works. But w/e, I'm off to school where the compilers work.

dppwdrmn26
02-12-04, 05:45 PM
Originally posted by youngbuck
result = -b + sqrt(pow(b, 2) - 4*a*c)/ 2*a;

You NEED paranthesis around the last part, they MUST be there or it CANNOT work, because the computer WILL follow order of operations. It is also a good idea to put the first part in paranthesis just to be sure, though it isnt necesary.

It needs to look like this:
result=(-b + sqrt(pow(b, 2)-4*a*c))/(2*a);
This should make it work.

Your way would have the first part divided by 2, then multiplied by a, which is obviously going to give you a wrong answer.

Cowboy Shane
02-12-04, 06:53 PM
Originally posted by youngbuck
What about a=6, b=3, and c=3? I get -1.#IND. And btw, what does that mean?

Look at the square root section of the equation, and plug the numbers in by hand (or your head) rather than trying to rely on the computer:

sqrt((b*b) - (4*a*c))
--> sqrt (9 - 72)
--> sqrt (-63)

Now, what happens when you try to get the square root of a negative number? You have studied imaginary numbers, right?

Roof Jumper
02-13-04, 07:31 AM
or just let us presume he doesnt, the square root of a negative number is undefined.

youngbuck
02-18-04, 03:57 PM
Originally posted by dppwdrmn26

You NEED paranthesis around the last part, they MUST be there or it CANNOT work, because the computer WILL follow order of operations. It is also a good idea to put the first part in paranthesis just to be sure, though it isnt necesary.

It needs to look like this:
result=(-b + sqrt(pow(b, 2)-4*a*c))/(2*a);
This should make it work.

Your way would have the first part divided by 2, then multiplied by a, which is obviously going to give you a wrong answer.

Yea, my teacher pointed that out... thats all I had wrong. I thought that I had already tried that a few times, but evidently I did not.

Cowboy Shane:
It was for an assignment at school where my teacher checked my code, and I needed to use the sqrt and pow functions. And yes, I've studied imaginary numbers.