Quadratic Formula Woes

F

fb

Hello everyone. I'm having a touch of trouble solving a problem using
the quadratic formula. I get a domain error...Somewhere in the sqrt
function I think. Could you guys give me a hint on what's up? Is there
maybe some kind of standard Quadratic function hiding away in the
libraries? That would be nice to have...Here's my source:

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */

d = sqrt(b*b-4*a*c);
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);

/* Display output */

printf("\nx1 = %e x2 = %e", x1, x2);

return EXIT_SUCCESS;
}
 
A

Arthur J. O'Dwyer

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");

Here you need a
fflush(stdout);
scanf("%f", &a);

This should be
scanf("%lf", &a);
and it's probably what's causing your troubles.

The same two comments apply twice again below.
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */

Before doing anything else, you should be printing out the values
of 'a', 'b', and 'c', just to make sure you read them correctly.
Printing intermediate results is an important debugging technique
in /any/ language.
d = sqrt(b*b-4*a*c);

If the quadratic has no real-number solutions, this will cause
a domain error. Could your problem be simply that you're trying
to solve an unsolvable quadratic?
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);

/* Display output */

printf("\nx1 = %e x2 = %e", x1, x2);

Better use "%g" unless you know what you're doing. And you
need to end the output with a newline.

printf("\nx1 = %g x2 = %g\n", x1, x2);
return EXIT_SUCCESS;
}

HTH,
-Arthur
 
C

CBFalconer

fb said:
Hello everyone. I'm having a touch of trouble solving a problem
using the quadratic formula. I get a domain error...Somewhere in
the sqrt function I think. Could you guys give me a hint ... snip

What is the square root of -1?
 
K

Karthik Kumar

fb said:
Hello everyone. I'm having a touch of trouble solving a problem using
the quadratic formula. I get a domain error...Somewhere in the sqrt
function I think. Could you guys give me a hint on what's up? Is there
maybe some kind of standard Quadratic function hiding away in the
libraries? That would be nice to have...Here's my source:

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */

d = sqrt(b*b-4*a*c);

You are making the assumption that b^2 - 4ac is always >= 0 .
May be you need to be a guard here, that checks in case they end up
being -ve numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top