225k views
0 votes
write the lines of code that compare two floating point numbers. return true when the left hand side (lhs) and the right hand side (rhs) are within epsilon, and false otherwise.

User Meza
by
6.8k points

1 Answer

2 votes

Answer:

#include <iostream>

bool compare_floats(double lhs, double rhs, double epsilon) {

return (lhs - rhs < epsilon) && (rhs - lhs < epsilon);

}

int main() {

double lhs, rhs, epsilon;

std::cout << "Enter the value of lhs: ";

std::cin >> lhs;

std::cout << "Enter the value of rhs: ";

std::cin >> rhs;

std::cout << "Enter the value of epsilon: ";

std::cin >> epsilon;

if (compare_floats(lhs, rhs, epsilon)) {

std::cout << "The numbers are within epsilon of each other." << std::endl;

} else {

std::cout << "The numbers are not within epsilon of each other." << std::endl;

}

return 0;

}

Step-by-step explanation:

This program prompts the user to enter the values of lhs, rhs, and epsilon, and then calls the compare_floats function to compare the numbers

User Vahshi
by
7.3k points