Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Quadratic Formula Woes

Reply
Thread Tools

Quadratic Formula Woes

 
 
fb
Guest
Posts: n/a
 
      08-29-2004
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;
}

 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-30-2004

On Sun, 29 Aug 2004, fb wrote:
>
> /* 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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-30-2004
fb wrote:
>
> 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?

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

 
Reply With Quote
 
kal
Guest
Posts: n/a
 
      08-30-2004
CBFalconer <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

> What is the square root of -1?


What is -1?
 
Reply With Quote
 
dbtid
Guest
Posts: n/a
 
      08-30-2004
CBFalconer wrote:

> fb wrote:
>
>>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?
>


i


 
Reply With Quote
 
Arjan
Guest
Posts: n/a
 
      08-30-2004
In message <z7EYc.52627$(E-Mail Removed)>
dbtid <(E-Mail Removed)> wrote:

> CBFalconer wrote:
>
> > fb wrote:
> >
> >>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?
> >

>
> i
>
>



tsk, tsk... Not true, but i squared equals -1



Arjan
 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      09-01-2004
fb wrote:

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

> x1 = (-b + d) / (2 * a);
> x2 = (-b - d) / (2 * a);
>
> /* Display output */
>
> printf("\nx1 = %e x2 = %e", x1, x2);
>
> return EXIT_SUCCESS;
> }
>



--
Karthik.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quadratic curve fitting! Vinodh Kumar C++ 6 12-01-2010 12:30 PM
hash table quadratic probing help please Totti Java 5 12-22-2007 09:17 PM
Quadratic Optimization Problem amitsoni.1984@gmail.com Python 3 11-22-2006 10:45 PM
Multiple Polynomial Quadratic Sieve Philip Smith Python 2 05-30-2006 10:29 PM
Quadratic Solutions in C++ Sunny Computer Support 12 02-26-2006 11:14 AM



Advertisments