Guest User

Untitled

a guest
Nov 17th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define PRECISION "20"
  7.  
  8. const double g_eps = 1e-9;
  9. const double g_magnitude = 1e+6;
  10.  
  11. const char g_no_solutions[] = "No solutions";
  12. const char g_infinite_num_of_solutions[] = "Infinite number of solutions";
  13. const char g_single_root_format[] = "x = %." PRECISION "lf\n";
  14. const char g_two_roots_format[] = "x1 = %." PRECISION "lf, " \
  15. "x2 = %." PRECISION "lf\n";
  16.  
  17. static inline bool eq(double u, double v) {
  18. return fabs(u - v) < g_eps;
  19. }
  20.  
  21. static inline bool lt(double u, double v) {
  22. return u + g_eps < v;
  23. }
  24.  
  25. static inline bool gt(double u, double v) {
  26. return lt(v, u);
  27. }
  28.  
  29. int main() {
  30. double a, b, c;
  31. scanf("%lf%lf%lf", &a, &b, &c);
  32. if (eq(a, 0.0)) {
  33. if (eq(b, 0.0)) {
  34. if (eq(c, 0.0))
  35. printf("%s\n", g_infinite_num_of_solutions);
  36. else
  37. printf("%s\n", g_no_solutions);
  38. } else {
  39. double x = -c / b;
  40. printf(g_single_root_format, x);
  41. }
  42. } else {
  43. double d = b * b - 4 * a * c;
  44. if (lt(d, 0.0))
  45. printf("%s\n", g_no_solutions);
  46. else if (eq(d, 0.0)) {
  47. double x = -b / (2 * a);
  48. printf(g_single_root_format, x);
  49. } else {
  50. double x1 = (-b - sqrt(d)) / (2 * a);
  51. double x2 = (-b + sqrt(d)) / (2 * a);
  52. double r1 = 2 * c / (-b + sqrt(d));
  53. double r2 = 2 * c / (-b - sqrt(d));
  54. if (b * b / g_magnitude > fabs(4 * a * c)) {
  55. /*
  56. * b * b >> 4 * a * c
  57. */
  58. if (gt(b, 0.0))
  59. printf(g_two_roots_format, x1, r2);
  60. else
  61. printf(g_two_roots_format, r1, x2);
  62. } else {
  63. printf(g_two_roots_format, x1, x2);
  64. }
  65. }
  66. }
  67. return 0;
  68. }
Add Comment
Please, Sign In to add comment