1

I have a question about types in C and I think it has to do with "real's"... This program is a quadratic equation solver and the user inputs a, b, and c in terms of ax^2 + bx + c = 0. My program works and I am sorry for lake of comments in the code so I will try to be very specific in my question. If you enter say 2, 2, 2 the discriminate of the quadratic is negative meaning no real answers or "imaginary numbers" (oh good old algebra days). So when you do this you get something like this enter image description here

Specific part in code where this happens: (First else in the while loop)

discriminate = b*b - 4 * a*c;
            if (discriminate < 0)
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                printf("\nNOTE: Roots are not real.\n");
                printf("The roots are, %.3f, and %.3f\n", root1, root2);
                break;
            }

So my question is two parts. 1) What is -1.#IO, and -1.#IO mean? (I know what it means) but what is #IO 2) How can I display the number properly? Is there a way?

FULL CODE:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main(void)
{
    //declarations
    float a;
    float b;
    float c;
    float root1, root2;
    int count;
    float discriminate;

    //Initialization
    count = 0;

    //starting propmts
    printf("\nHello, this program will compute the real roots");
    printf("of a quadratic equation.\n");
    printf("In terms of a(x)^2 + b(x) + c = 0\n");
    printf("\nPlease enter in the \"a\" value: ");
    scanf("%f", &a);
    printf("Please enter in the \"b\" value: ");
    scanf("%f", &b);
    printf("Please enter in the \"c\" value: ");
    scanf("%f", &c);


    while (count == 0)
    {
        if (a == 0)
        {
            if (a == 0 && b == 0)
            {
                printf("There is no soultion...\n");
                break;
            }
            else
            {
                root1 = (-c / b);
                printf("\nNOTE: Input is not quadratic but uesing \"(-c / b)\" ");
                printf("the root is %.3f\n", root1);
                break;
            }   
        }
        else
        {
            discriminate = b*b - 4 * a*c;
            if (discriminate < 0)
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                printf("\nNOTE: Roots are not real.\n");
                printf("The roots are, %.3f, and %.3f\n", root1, root2);
                break;
            }
            else
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                if (root1 == root2)
                {
                    printf("The root is, %.3f.\n", root1);
                    break;
                }
                else
                {
                    printf("The roots are, %.3f, and %.3f.\n", root1, root2);
                    break;
                }

            }
        }
    }
    printf("Goodbye.\n");
    return 0;
}
T.Malo
  • 512
  • 1
  • 7
  • 24
  • 2
    Well if discriminate is negative you can not take a square root out of it , I also dont understand you : why would you try and print answers on negative discriminate which means you dont have REAL number roots for this. The only answer you can get on this would be Complex number answer. ofc float won't represent it . – Coldsteel48 Mar 22 '14 at 03:18
  • 1
    http://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c here is couple examples for complex numbers caculations in C – Coldsteel48 Mar 22 '14 at 03:22

3 Answers3

1

If the discriminant is less than zero, then you have some extra work to do. With the current code, you take the square root of a negative number, and the result should be not-a-number (NAN). I'm not sure why the printf doesn't just say that.

To fix the problem, you need to take the square root of the negative of the discriminant. Then you need to calculate the real and imaginary parts of the answer and display them as a complex number. Note that printf doesn't have any built-in support for complex numbers, so you have format the number yourself, e.g.

printf( "%f + %f i", realpart, imagpart );
user3386109
  • 34,287
  • 7
  • 49
  • 68
1

If the discriminant is less than zero, then you have 2 complex roots. If the discriminant is greater than zero, then you have 2 real roots. If the discriminant is zero, then you have one real root.

        if (discriminate < 0)
        {
            float rootr = -b / (2 * a);
            float rooti = sqrt(-discriminate) / (2 * a);
            printf("\nNOTE: Roots are not real.\n");
            printf("The roots are, %.3f + %.3f i, and %.3f - %.3f i\n",
                rootr, rooti,
                rootr, rooti);
            break;
        }
        else if(discriminate > 0)
        {
            float s = sqrt(discriminate);
            float root1 = (-b + s) / (2 * a);
            float root2 = (-b - s) / (2 * a);
            printf("The roots are, %.3f, and %.3f.\n", root1, root2);
            break;
        }
        else
        {
            float root = -b / (2 * a);
            printf("The root is, %.3f.\n", root);
            break;
        }
pat
  • 12,587
  • 1
  • 23
  • 52
1

enter image description here

MY CODE:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
   float a,b,c,d,x,y,i,j;
   clrscr();
   printf("\t\t\t QUADRATIC EQUATION SOLVING\n");
   printf("Enter the co-efficients of x^2,x and constant \n");
   scanf("%f%f%f",&a,&b,&c);
   d=(b*b)-(4*a*c);
   if(d>=0)
   {
      x=(-b+sqrt(d))/(2*a);
      y=(-b-sqrt(d))/(2*a);
      printf("The roots of the equation are %.2f %.2f",x,y);
   }
   else
   {
      d*=-1;
      i=b/(2*a);
      j=sqrt(d)/(2*a);
      printf("The roots are %.2f+%.2fi and %.2f-%.2fi",i,j,i,j);
   }
   getch();
}
Muthusamy
  • 56
  • 9
  • Not sure why conio.h is needed. What does it do in your code? Mine work with out it. – T.Malo Mar 22 '14 at 15:01
  • @T.Malo since I am working in Turbo C++ , conio.h is required in order to use the functions clrscr() and getch() – Muthusamy Mar 22 '14 at 16:23