Write a function called assert_within_tolerance() which has three float paramete
ID: 3758935 • Letter: W
Question
Write a function called assert_within_tolerance() which has three float parameters. The function assert_within_tolerance () returns True if the first two parameters are close in value to each other, that is, if they are within +/- the third parameter of each other. Otherwise assert_within_tolerance () should return False. (In engineering, the third parameter is called the tolerance.) Note: if the difference equals the tolerance, return True. Required parameter testing: all three passed values must be floats, and the tolerance must be positive; otherwise your function should print a meaningful error message and return None.
Explanation / Answer
I think This below logic will cover all possible combinations of tolerance value checking using three floating point variables given in the problem statement . If you need still improvements in code let us know.
bool assert_within_tolerance(float num1, float num2, float tolerance)
{
bool flag;
if(tolerance < 0)
printf("Tolerance Must be Positive");
if( abs((num1 + num2)) => tolerance || abs((num1 - num2)) <= tolerance)
return true;
else if( abs((num1 - num2)) >= tolerance || abs((num1 + num2)) <= tolerance)
return true;
else if( (num1 - num2) == tolerance )
return true;
else if (( (num1 + num2) == tolerance ))
return true;
else
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.