/*--- Attempt at a C++ version of BSL Racket's check-within compile using: g++ demo.cpp -o demo run using: ./demo can redirect its output to a file using: ./demo > demo-out.txt by: Sharon Tuttle last modified: 2025-10-16 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- signature: check_within: double double double -> bool purpose: expects a function call, its expected value, and the maximum difference there can be between their values to be be considered "close enough", and returns true if the absolute value of the difference of the function call and its expected value is less than that maximum difference tests: check_within(sqrt(4), 2.0, 0.1) == true check_within(sqrt(2), 1.4, 0.001) == false // attempt at a a boundary test -- if equal to maximum // difference, check_within will return false check_within(sqrt(9), 3.0, 0.0) == false ---*/ bool check_within(double funct_call, double expected_val, double max_diff) { return abs(funct_call - expected_val) < max_diff; } /*--- test the function above ---*/ int main() { cout << boolalpha; cout << "*** Testing: check_within ***" << endl; cout << (check_within(sqrt(4), 2.0, 0.1) == true) << endl; cout << (check_within(sqrt(2), 1.4, 0.001) == false) << endl; cout << (check_within(sqrt(9), 3.0, 0.0) == false) << endl; return EXIT_SUCCESS; }