Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define PRECISION "20"
- const double g_eps = 1e-9;
- const double g_magnitude = 1e+6;
- const char g_no_solutions[] = "No solutions";
- const char g_infinite_num_of_solutions[] = "Infinite number of solutions";
- const char g_single_root_format[] = "x = %." PRECISION "lf\n";
- const char g_two_roots_format[] = "x1 = %." PRECISION "lf, " \
- "x2 = %." PRECISION "lf\n";
- static inline bool eq(double u, double v) {
- return fabs(u - v) < g_eps;
- }
- static inline bool lt(double u, double v) {
- return u + g_eps < v;
- }
- static inline bool gt(double u, double v) {
- return lt(v, u);
- }
- int main() {
- double a, b, c;
- scanf("%lf%lf%lf", &a, &b, &c);
- if (eq(a, 0.0)) {
- if (eq(b, 0.0)) {
- if (eq(c, 0.0))
- printf("%s\n", g_infinite_num_of_solutions);
- else
- printf("%s\n", g_no_solutions);
- } else {
- double x = -c / b;
- printf(g_single_root_format, x);
- }
- } else {
- double d = b * b - 4 * a * c;
- if (lt(d, 0.0))
- printf("%s\n", g_no_solutions);
- else if (eq(d, 0.0)) {
- double x = -b / (2 * a);
- printf(g_single_root_format, x);
- } else {
- double x1 = (-b - sqrt(d)) / (2 * a);
- double x2 = (-b + sqrt(d)) / (2 * a);
- double r1 = 2 * c / (-b + sqrt(d));
- double r2 = 2 * c / (-b - sqrt(d));
- if (b * b / g_magnitude > fabs(4 * a * c)) {
- /*
- * b * b >> 4 * a * c
- */
- if (gt(b, 0.0))
- printf(g_two_roots_format, x1, r2);
- else
- printf(g_two_roots_format, r1, x2);
- } else {
- printf(g_two_roots_format, x1, x2);
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment