Unit 3.6 Checking for Reasonably Close To

PROG

Read in three numbers. See if the sum of these three is reasonably close to the fourth.

PED

To learn how to write code to check if two numbers are reasonably close to each other.

CONCEPT

To check if number1 is reasonably close to number2
epsilon is closeness factor, the smaller epsilon is, the closer they are.
Usual values for epsilon 0.01 down 0.0001

if ((number1-number2)/number2 > -epsilon) and
((number1-number2)/number2 < epsilon) then begin
statements to do if number1 is close to number2
end;



Unit 3.6 Checking closeness

Click here to view Unit 3.6 Pascal program.

Real numbers are not exact. If I add up three variables, each of which contains 0.1 and then compare that sum to 0.3 with "=", I might find that they are not equal. Thsi is because numbers are represented in the computer in binary. In the binary system 0.1 is a repeating number like 0.3333 is a repeating number. On a calculator, if I was to add up 0.33333... 3 times, I would not get 1 exactly. I would get something close. Same, in a computer, if I added up 0.1 in binary three times, it wouldn't add up.

Just as importantly, numbers on the computer often represent physical quantities. For example, they may have been input as data from a surveyor's instruments and one desires to check if the four sides of an alleged square being measured are really equal. They may represent weights of matter and one desires to verify if the law of conservation of mass is really being obeyed. Of course, these comparisons would never be equal, on a computer or otherwise. We must check equality for "close enough."

What does it mean for the quantity a to be close enough to b, so that we can say they are equal. It means that a is going to be in the range starting at just a little bit before b and ending a little bit after b. We can thus apply the plan in Unit 3.5 for check if a is between b - epsilon and b+epsilon and get some code looking like this:

      if (a> b-epsilon) and (a< b+epsilon) then begin
            statements to execute if a is close enough
      endif 
and choose epsilon to be an appropriate value. For many applications, this is good enough.

However, the code in the template on the previous page is more general. Close enough is different depending upon the quantities being compared. If we are looking at astronomical distances measured in miles, close enough might be 500 miles in doing something like checking whether Kepler's laws or Newton's laws hold--particularly, if we are measuring distances using a home telescope. On the other hand, if we were checking data taken with a micrometer, close enough would be of the order of 0.001 or so. Thus, the template compares the difference of the two numbers divided by one of the two numbers with epsilon. That way epsilon is automatically scaled depending upon the quantities being examined. We can thus make our code more general.

Note: We only have this problem with real numbers. Integer's on the computer, that is boxes declared with ":integer" in the var statement, don't have this problem. If we hadd 1 + 1 + 1, we will definitely get 3 here. It is only real numbers, where numbers aren't quite exact. Of course, if we are representing real world quantities that are measured with some kind of instrument as an integer, we might still want to consider close enough. Businessmen who keep track of money, get upset if when your programs start losing pennies, even when measuring billions of dollars worth of money.