C/C++

Как сравнить числа с тонностью до 3 знаков после запятой в c++

Сравнение действительных чисел
производить с точностью до 3 знаков после запятой.
Не совсем понятно, что нужно, округлять до третьего знака после запятой либо отрезать всё, что после третьего знака после запятой. Здесь оба варианта.

#include <iostream>
#include <iomanip>
using namespace std;
struct Comparator {
public:
static bool greater_cut(const double a, const double b) {
auto [ax, ay] = cut(a);
auto [bx, by] = cut(b);
if (ax > bx) return true;
if (ax == bx && ay > by) return true;
return false;
}
static bool less_cut(const double a, const double b) {
auto [ax, ay] = cut(a);
auto [bx, by] = cut(b);
if (ax < bx) return true;
if (ax == bx && ay < by) return true;
return false;
}
static bool equal_cut(const double a, const double b) {
auto [ax, ay] = cut(a);
auto [bx, by] = cut(b);
return ax == bx && ay == by;
}
static bool greater_round(const double a, const double b) {
auto [ax, ay] = round(a);
auto [bx, by] = round(b);
if (ax > bx) return true;
if (ax == bx && ay > by) return true;
return false;
}
static bool less_round(const double a, const double b) {
auto [ax, ay] = round(a);
auto [bx, by] = round(b);
if (ax < bx) return true;
if (ax == bx && ay < by) return true;
return false;
}
static bool equal_round(const double a, const double b) {
auto [ax, ay] = round(a);
auto [bx, by] = round(b);
return ax == bx && ay == by;
}
static void epsilon(const int sgn) {
sign = sgn;
}
private:
inline static int sign = 2;
static pair<long long, long long> cut(const double x) {
auto a = static_cast<long long>(x);
auto b = static_cast<long long>((x - a) * pow(10, sign));
return { a, b };
}
static pair<long long, long long> round(const double x) {
auto a = static_cast<long long>(x);
auto b = static_cast<long long>((x - a) * pow(10, sign));
auto n = static_cast<long long>((x - a) * pow(10, sign + 1)) - b * 10;
if (n >= 5) ++b;
return { a, b };
}
};
int main() {
const double a = 10.1234;
const double b = 10.1229;
const double c = 10.1241;
const double d = 10.123999999999999;
const double e = 10.122999999999999;
cout << fixed << setprecision(4)
<< "a: " << a << '\n'
<< "b: " << b << '\n'
<< "c: " << c << '\n'
<< setprecision(15)
<< "d: " << d << '\n'
<< "e: " << e << '\n';
const int sign = 3;
Comparator cmp;
cmp.epsilon(sign);
puts("Cutting off:");
if (Comparator::less_cut(a, b)) puts("a < b");
if (Comparator::greater_cut(a, b)) puts("a > b");
if (Comparator::equal_cut(a, b)) puts("a == b");
if (Comparator::less_cut(a, c)) puts("a < c");
if (Comparator::greater_cut(a, c)) puts("a > c");
if (Comparator::equal_cut(a, c)) puts("a == c");
if (Comparator::less_cut(a, d)) puts("a < d");
if (Comparator::greater_cut(a, c)) puts("a > d");
if (Comparator::equal_cut(a, d)) puts("a == d");
if (Comparator::less_cut(a, e)) puts("a < e");
if (Comparator::greater_cut(a, e)) puts("a > e");
if (Comparator::equal_cut(a, e)) puts("a == e");
puts("Rounded:");
if (Comparator::less_round(a, b)) puts("a < b");
if (Comparator::greater_round(a, b)) puts("a > b");
if (Comparator::equal_round(a, b)) puts("a == b");
if (Comparator::less_round(a, c)) puts("a < c");
if (Comparator::greater_round(a, c)) puts("a > c");
if (Comparator::equal_round(a, c)) puts("a == c");
if (Comparator::less_round(a, d)) puts("a < d");
if (Comparator::greater_round(a, c)) puts("a > d");
if (Comparator::equal_round(a, d)) puts("a == d");
if (Comparator::less_round(a, e)) puts("a < e");
if (Comparator::greater_round(a, e)) puts("a > e");
if (Comparator::equal_round(a, e)) puts("a == e");
system("pause > nul");
}
Эдуард Новоселов
Эдуард Новоселов
95 446
Лучший ответ
Абсолютная величина разности чисел меньше одной тысячной - числа считаются равными
if( (int) (a * 1000) < (int) (b * 1000) )
Иван Куликов
Иван Куликов
55 632
Евгений Леонтьев Для чисел более 2 миллионов результат будет неправильныц
if (fabs(a-b)<0.001)
ИП
Иванов Павел
75 079