1.#IND00 error in equasion solver

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cecnik
    New Member
    • Jun 2014
    • 7

    1.#IND00 error in equasion solver

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

    int main()
    {
    float a, b, c, D, x1, x2, j, x1real, x1imag;



    printf("S pomocjo tega programa lahko izracunamo kvadratno enacbo oblike ax^2+bx+c=0\n") ;
    printf("vnesi prvo (a) spremenljivko enacbe: \n");
    scanf("%f", &a);
    printf("vnesi drugo (b) spremenljivko enacbe: \n");
    scanf("%f", &b);
    printf("vnesi tretjo (c) spremenljivko enacbe: \n");
    scanf("%f", &c);

    D=(b*b)-(4*a*c);

    if(D>=0)
    {
    x1=((-b+sqrt(D))/(2*a));
    x2=((-b-sqrt(D))/(2*a));
    printf("Resitvi kvadratne enacbe sta:\n");
    printf("x1=%f\n ", x1);
    printf("x2=%f\n ", x2);
    }
    else
    {
    x1real=-b/2*a;
    x1imag=sqrt(D)/2*a;
    printf("Rešitve enačbe so:\n");
    printf("x1 = %f + i*%f\n",x1real, x1imag);
    printf("x1 = %f - i*%f\n",x1real, x1imag);

    }



    system("PAUSE") ;
    return 0;
    }
    So this is the code I wrote, it is supposed to be a program that solves quadratic equations (don't mind the printf sections with tekst in my language, it's a school asignment..). so aniway, when you run it and you solve an equasion with a negative discriminant the irational part is shown as 1.#IND00.. I get that this is an error that ocurs because the size of the number (places of numeric signs) is too big, but how can I reduce it?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Most likely the variable D passed to sqrt is negative since it can't be >=0. Not possible to take the square root of a negative number.

    Comment

    • cecnik
      New Member
      • Jun 2014
      • 7

      #3
      shit.. can't believe I missed that.
      It used to say "-D" in the brackets but then I changed some stuff around and must have missed it.

      thanks man

      Comment

      Working...