Quadratic Formula Woes

Discussion in 'C Programming' started by fb, Aug 30, 2004.

  1. fb

    fb Guest

    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;
    }
    fb, Aug 30, 2004
    #1
    1. Advertising

  2. 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
    Arthur J. O'Dwyer, Aug 30, 2004
    #2
    1. Advertising

  3. fb

    CBFalconer Guest

    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?
    CBFalconer, Aug 30, 2004
    #3
  4. fb

    kal Guest

    CBFalconer <> wrote in message news:<>...

    > What is the square root of -1?


    What is -1?
    kal, Aug 30, 2004
    #4
  5. fb

    dbtid Guest

    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

    :p
    dbtid, Aug 30, 2004
    #5
  6. fb

    Arjan Guest

    In message <z7EYc.52627$>
    dbtid <> 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
    >
    > :p



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



    Arjan
    Arjan, Aug 30, 2004
    #6
  7. 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.
    Karthik Kumar, Sep 1, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinodh Kumar

    Quadratic curve fitting!

    Vinodh Kumar, Aug 11, 2003, in forum: C++
    Replies:
    6
    Views:
    5,198
    onetimeuser
    Dec 1, 2010
  2. Philip Smith

    Multiple Polynomial Quadratic Sieve

    Philip Smith, May 30, 2006, in forum: Python
    Replies:
    2
    Views:
    517
    Philip Smith
    May 30, 2006
  3. Replies:
    3
    Views:
    428
  4. Totti
    Replies:
    5
    Views:
    1,411
    Totti
    Dec 22, 2007
  5. Replies:
    10
    Views:
    530
    Aaron Watters
    Jun 18, 2008
Loading...

Share This Page