This equation does not have a sufficiently accurate solution. And the program below demonstrates this. That is, all the results of the calculation of the equation have large errors.
#include <iostream> #include <limits> #include <cmath> int main() { const int N = 200; auto first_exp = []( int x, int y ) { return 5.0 / ( x + y ); }; auto second_exp = []( int x, int y ) { return 2.0 / ( x - y ); }; bool found = false; int x = 0, y = 0; for ( ; !found && x < N; x++ ) { for ( ; !found && y < N; y++ ) { if ( x != y ) { found = std::abs( first_exp( x, y ) - second_exp( x, y ) ) < std::numeric_limits<double>::epsilon(); } } } if ( found ) { std::cout << "x = " << x << ", y = " << y << std::endl; } else { std::cout << "There is no solution in the range 0 - " << N << std::endl; } return 0; } There is no solution in the range 0 - 200
Therefore, it is better to write the program so that it does not find the exact solution, but the best of all x and y values on the interval [0, 200] . In this case, the program may look like this.
#include <iostream> #include <cmath> int main() { const int N = 200; auto first_exp = []( int x, int y ) { return 5.0 / ( x + y ); }; auto second_exp = []( int x, int y ) { return 2.0 / ( x - y ); }; auto value = [&]( int x, int y ) { return std::abs( first_exp( x, y ) - second_exp( x, y ) ); }; bool first = true; int best_x = 0, best_y = 0; for ( int x = 0; x < N; x++ ) { for ( int y = 0; y < N; y++ ) { if ( x != y ) { if ( first || value( x, y ) < value( best_x, best_y ) ) { best_x = x; best_y = y; first = false; } } } } std::cout << "The best values are x = " << best_x << ", y = " << best_y << std::endl; return 0; }
The output of the program to the console:
The best values are x = 7, y = 3
for(x = 0; x < 200; x++){? - Igor