0

I am trying to build a small quadratic calculator in C, but I am getting the following result no matter what my input is....

enter image description here

Here is my code:

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

int main(void) {
float a, b, c;

printf("Enter a:");
scanf("%f", &a);
printf("Enter b:");
scanf("%f", &b);
printf("Enter c:");
scanf("%f", &c);

float discriminant = sqrt(b*b - 4*a*c);
float root1 = (-b + discriminant) / (2*a);
float root2 = (-b - discriminant) / (2*a);

printf("Root 1: %f\n", root1);
printf("Root 2: %f\n", root2);
}
slickset
  • 179
  • 13
  • 2
    `float discriminant = sqrt(b*b - 4*a*c);` this is taking the sqrt of -36 – Matthew Kerian Feb 23 '18 at 01:53
  • 2
    Learn some basic debugging. First, print out all your inputs. Then, print out the intermediate values. If you just have a big black box in between inputs and final answer, how do you expect to learn anything? Also, specific to this problem, "discriminant" is not the sqrt, it's the number inside the sqrt, and you have to check it for sign first. – Lee Daniel Crocker Feb 23 '18 at 01:54
  • You have to check if `b*b - 4*a*c` is negative. If it's negative, there is no solution to the equation. – Pablo Feb 23 '18 at 01:56
  • 1
    There are still two solutions, they're just complex numbers and have to be output differently. – Lee Daniel Crocker Feb 23 '18 at 01:56
  • My guess is that the OP is trying to have a solution in `|R`, not in `|C`. – Pablo Feb 23 '18 at 01:58
  • @Pablo I don't think OP was *trying* per se, since (s)he doesn't even realize that quadratic equations could potentially have complex roots. – meowgoesthedog Feb 23 '18 at 01:59
  • @meowgoesthedog and if the OP is still at school and hasn't learn complex numbers yet, he/she might not even know that `sqrt(-1)` is *i*. I don't see the point of arguing about that, most people in school have hard enough times dealing with solutions in `|R`. – Pablo Feb 23 '18 at 02:03
  • I don't know off the top of my head, are there any situations where that could be negative but come up with solutions on the real plane? – Matthew Kerian Feb 23 '18 at 02:06
  • Nope. There are only two roots, and all complex roots exist in conjugate pairs (true for a polynomial of any degree), so if one is real the other must also be, and vice versa. @MatthewKerian – meowgoesthedog Feb 23 '18 at 02:11
  • If you are working in `|R`, quadratic functions can have either 0 solutions (like `f(x) = x² + 1`), one solution (like `f(x) = (x-1)² = x² - 2x -1`) and 2 solutions (like `f(x) = x² - 1`). – Pablo Feb 23 '18 at 02:11
  • @Pablo + explain that these cases correspond to `b * b - 4 * a * c` < 0, = 0 and > 0 respectively. – meowgoesthedog Feb 23 '18 at 02:13
  • Thank you all so much! I realized that my program didn't handle complex numbers, but I couldn't tell what the output was trying to tell me. I thought it would output "nan" if it were imaginary. – slickset Feb 23 '18 at 02:19
  • @slickset, sorry, but the discriminant of a quadratic equation is `b*b - 4.0*a*c` and not the square root of that expression. If _the discriminant is negative_, then you have to consider the fact that you are having complex roots, and that's the problem with the data you present in the question. I don't know the kind of data you have feed to your program, apart of the one shown, but probably you fed always negative discrimintants. – Luis Colorado Feb 24 '18 at 09:58
  • regarding expressions like: `(2*a)` this is multiplying a `int` times a `float` To avoid conversion problems, suggest `(2.0f*a)` – user3629249 Feb 25 '18 at 03:17
  • when calling any of the `scanf()` family of functions: 1) always check the return value (not the parameter values) to assure the operation was successful. In the posted code, any returned value other than 1 indicates an error occurred – user3629249 Feb 25 '18 at 03:18

1 Answers1

2

float discriminant = sqrt(b*b - 4*a*c)

This line is potentially dangerous, as it'll sometimes do the square root of a negative number. If you want to be able to handle complex roots you'll have to custom build a solution for that.

What you can change with this is to just verify that input doesn't cause that to be negative.

Matthew Kerian
  • 812
  • 5
  • 18