-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
42 lines (38 loc) · 1.02 KB
/
quadratic.cpp
File metadata and controls
42 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <cmath>
void SolveQuadratic(int a, int b, int c) {
//Установка точности вывода в 6 символов
std::cout << std::setprecision(6);
double eps = 1e-3; // Погрешность при сравнении double
double x1{0}, x2{0};
if (a == 0 && b == 0 && c != 0) {
std::cout << "no solutions";
return;
}
if (a == 0 && b == 0 && c == 0) {
std::cout << "infinite solutions";
return;
}
if(b != 0 && a == 0){
x1 = -c / static_cast<double>(b);
std::cout << x1;
return;
}
long long d = static_cast<long long>(b) * b - 4 * static_cast<long long>(a) * c;
if(d < 0){
std::cout << "no solutions";
return;
}
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
if((x1 - x2) < eps){
std::cout << x1;
}
else{
if(x1 > x2){
std::swap(x1, x2);
}
std::cout << x1 << " " << x2;
}
}