I solve various kinds of Olympiad problems, when I worked in Pascal I never thought about the work of the operator "=" or "> =" in working with real numbers, I thought that with the accuracy of comparisons and problems there could not be. But I switched to C ++ and one teacher told me that "==" or "> =" cannot be used when working with real numbers in C, you need to write fabs(a - b) > eps
. And here is the question:
- Is it true?
- How to choose the right eps?
- And why could some tasks not pass all the tests with such a chip, and when I wrote simply "==" they passed.
Here is a specific example. The task that passed after I changed LessEqual
to "<=". Given a real number a and a natural n. Calculate the nth root of a. The program should display a single number: the answer to the problem with an accuracy of at least 6 decimal places
#include <cstdio> #include <iostream> #include <cmath> using namespace std; const double eps = 1e-8; const double eps2 = 1e-12; bool Equal(double a, double b) { return fabs(a - b) < eps2; } bool lessEqual(double a, double b) { return ((a < b) || Equal(a, b)); } double a; int n; void solve() { double l = 0, r = a + 1, mid = 0; while (r - l > eps) { mid = (l + r) / 2; if (lessEqual(pow(mid, n), a)) l = mid; else r = mid; } printf("%.7lf", l); } int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); scanf("%lf\n", &a); scanf("%d", &n); solve(); return 0; }