0

I am trying to write a code that solves quadratic equations. After entering the 3 constants of the equations, nothing happens. The programs ends even though there are conditions.

This is the code. Thank you for the help.

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

int main(){

    printf("Se va a trabajar con la ecuacion de la forma ax^2+bx+c\n\n" );


    float a,b,c,x_1,x_2,x_0;

    printf("Ingrese el valor de la constante a: ");
    scanf("%f", &a);

    printf("Ingrese el valor de la constante b: ");
    scanf("%f", &b);

    printf("Ingrese el valor de la constante c: ");
    scanf("%f", &c);

    double dis = b*b-4*a*c;

    double sqr = sqrt(dis);

    if(sqr<0){

        printf("No tiene solucion en los numeros reales");


    }



    if(sqr==0){

        x_0= -b/(2*a);

        printf("La solucion es %f", x_0);


    }

    if(sqr>0){

       x_1= (-b + sqr)/(2*a); 
       x_2= (-b - sqr)/(2*a); 

       printf("Las soluciones son %f y %f", x_1, x_2);

    }


}
melpomene
  • 84,125
  • 8
  • 85
  • 148
angelustt
  • 109
  • 1
  • 3
  • 9
  • 1
    what are values? – eyllanesc Oct 01 '17 at 22:18
  • 1
    Go back and check your math, you need to check if `dis` < 0, not `sqr`. – President James K. Polk Oct 01 '17 at 22:19
  • What is your input that causes the crash to happen? Where does the crash happen? You *have* tried to catch the crash in a debugger? Perhaps you should take some time to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn how to use a debugger. – Some programmer dude Oct 01 '17 at 22:20
  • There is no crash. After the input of the 3 constants, the program ends. It says "Press ENTER to continue". – angelustt Oct 01 '17 at 22:22
  • 1
    Why did you tag this `segmentation-fault` and `core` if there is no crash? – melpomene Oct 01 '17 at 22:23
  • If `dis < 0`, then `sqrt(dis)` evaluates to `NaN` and none of the branches evaluate to true... – Alexandre C. Oct 01 '17 at 22:23
  • Also, I recommend you check the answer to that question and use a more stable formula : https://stackoverflow.com/questions/4503849/quadratic-equation-in-ada – Alexandre C. Oct 01 '17 at 22:26
  • 1
    If you insist on writing questions with code / comments in spanish, you may want to consider https://es.stackoverflow.com/ instead. As it stands, it's a bit rude to be asking a question in an english-speaking forum and leaving your comments in a foreign language. I'm pretty sure if people answered your question in their own language, you wouldn't be too impressed. – Tasos Papastylianou Oct 01 '17 at 22:30
  • Read more about [MCVE] before asking here. – Basile Starynkevitch Oct 15 '17 at 16:53

2 Answers2

1

If dis < 0, then sqr = sqrt(dis) evaluates to not-a-number (NaN), and NaNs compare always false to anything (even NaN == NaN is false). Therefore, in this case, all your if conditions are false and nothing happens.

Correct your formula, and everything should be fine.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
1

sqrt returns NaN if the argument is negative, and comparing anything to NaN gives false. Hence, if dis is negative, none of your if-statements will be entered. Actually you should check dis instead of it's square root. But there is also a trick to "detect" Nan:

int main(int argc, char** argv) {

    double f = sqrt(-1);
    if (f != f) {
        printf("oh! Not A Number!");
    }
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58