Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this file is a function called 'equals' that doesn't quite work with floating point values due to round off errors. There are two examples,

In this file is a function called 'equals' that doesn't quite work with floating point values due to round off errors. There are two examples, one of which doesn't work. Do the following: 1. Why is the first example not working? 2. Fix the function 'equals' so that it works for both examples. We want the two numbers to be considered equal if they are equal in the first 3 decimals after the decimal point. NOTE: There are several ways to do this. You can take the absolute value of the difference between a and b and compare it to a tolerance value. In other words, if |a-b| < tol is true, then we can say the values are 'equal'. The tolerance value should be just small enough to act as a 'cutoff' point for equality but no smaller.

CODE

================================================

/* PUT YOUR NAME HERE * Put answers to * questions here. * * */

#include #include #include #include

int equals(float a, float b) { if(a == b) return 1; return 0; }

void example1() { float a=100.6; float b=100.0; for(int i=0; i < 6; i++) { a = a - .1; } printf("Example 1: these should equal each other "); if(equals(a, b)) { printf("%f and %f are equal. ", a, b); } else { printf("%f and %f are NOT equal. ", a, b); } }

void example2() { float a=100.006; float b=100.0; for(int i=0; i < 7; i++) { a = a - .001; } printf("Example 1: these should NOT equal each other "); if(equals(a, b)) { printf("%f and %f are equal. ", a, b); } else { printf("%f and %f are NOT equal. ", a, b); } }

int main() { example1(); example2(); return 0; } ==========================================

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

Revise the basic concepts of turnover and define turnover intent

Answered: 1 week ago

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

=+ What is the role of government in bargaining?

Answered: 1 week ago

Question

=+ Who is the negotiation partner at company level?

Answered: 1 week ago

Question

=+Which associations exist?

Answered: 1 week ago