How to resolve the algorithm Approximate equality step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Approximate equality step by step in the Mathematica/Wolfram Language programming language

Table of Contents

Problem Statement

Sometimes, when testing whether the solution to a task (for example, here on Rosetta Code) is correct, the difference in floating point calculations between different language implementations becomes significant. For example, a difference between 32 bit and 64 bit floating point calculations may appear by about the 8th significant digit in base 10 arithmetic.

Create a function which returns true if two floating point numbers are approximately equal.

The function should allow for differences in the magnitude of numbers, so that, for example, 100000000000000.01   may be approximately equal to   100000000000000.011, even though   100.01   is not approximately equal to   100.011. If the language has such a feature in its standard library, this may be used instead of a custom function. Show the function results with comparisons on the following pairs of values:

Answers should be true for the first example and false in the second, so that just rounding the numbers to a fixed number of decimals should not be enough. Otherwise answers may vary and still be correct. See the Python code for one type of solution.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Approximate equality step by step in the Mathematica/Wolfram Language programming language

This code is written in the Wolfram Language and is used to test whether two numbers are close enough to each other within a given tolerance.

The CloseEnough function takes three arguments: a and b, the two numbers to be compared, and tol, the tolerance. The function returns True if the difference between a and b is less than or equal to tol, and False otherwise.

The numbers variable is a list of pairs of numbers. The first number in each pair is the expected value, and the second number is the actual value.

The code uses the Map function to apply the CloseEnough function to each pair of numbers in the numbers list. The @@@ operator is used to apply the Map function to each element of the numbers list, and the Grid function is used to display the results in a table.

The output of the code is a table with three columns: the expected value, the actual value, and the result of the CloseEnough function. The CloseEnough function returns True for all of the pairs of numbers, which indicates that the actual values are all within the specified tolerance of the expected values.

Source code in the wolfram programming language

ClearAll[CloseEnough]
CloseEnough[a_, b_, tol_] := Chop[a - b, tol] == 0
numbers = {
   {100000000000000.01, 100000000000000.011},
   {100.01, 100.011},
   {10000000000000.001/10000.0, 1000000000.0000001000},
   {0.001, 0.0010000001},
   {0.000000000000000000000101, 0.0},
   {Sqrt[2.0] Sqrt[2.0], 2.0}, {-Sqrt[2.0] Sqrt[2.0], -2.0},
   {3.14159265358979323846, 3.14159265358979324}
   };
(*And@@Flatten[Map[MachineNumberQ,numbers,{2}]]*)
{#1, #2, CloseEnough[#1, #2, 10^-9]} & @@@ numbers // Grid


  

You may also check:How to resolve the algorithm Search a list step by step in the Lasso programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Factor programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the WDTE programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the PL/I programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the Raku programming language