• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Trouble with quadratic formula

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

youngbuck

Member
Joined
Aug 30, 2002
Location
CO, USA
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...

Thanx in advance,

-YB
 
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!
 
youngbuck said:



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
 
Re: Re: Trouble with quadratic formula

deRusett said:



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.
 
Last edited:
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)..."
 
Re: Re: Re: Trouble with quadratic formula

youngbuck said:


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

Code:
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.
 
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
 
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).
 
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.
 
Last edited:
youngbuck said:
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.
 
youngbuck said:
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?
 
Re: Re: Trouble with quadratic formula

dppwdrmn26 said:


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