How to resolve the algorithm Approximate equality step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Approximate equality step by step in the XPL0 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 XPL0 programming language

Source code in the xpl0 programming language

func ApproxEqual(A, B);         \Return 'true' if approximately equal
real A, B;
real Epsilon;
[Epsilon:= abs(A) * 1E-15;
return abs(A-B) < Epsilon;
];

real Data;
int  I;
[Format(0, 16);
Data:=[ [100000000000000.01,   100000000000000.011],    \should return true
        [100.01,   100.011],                            \should return false
        [10000000000000.001 / 10000.0,   1000000000.0000001000],
        [0.001,   0.0010000001],
        [0.000000000000000000000101,   0.0],            \is undefined
        [sqrt(2.0) * sqrt(2.0),    2.0],
        [-1.0 * sqrt(2.0) * sqrt(2.0),   -2.0],         \-sqrt doesn't compile!
        [3.14159265358979323846,   3.14159265358979324] ];
for I:= 0 to 7 do
        [IntOut(0, I+1);  Text(0, ". ");
        RlOut(0, Data(I,0));  ChOut(0, ^ );  RlOut(0, Data(I,1));
        Text(0, if ApproxEqual(Data(I,0), Data(I,1)) then " true" else " false");
        CrLf(0);
        ];
]

  

You may also check:How to resolve the algorithm Forest fire step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Forth programming language
You may also check:How to resolve the algorithm Sockets step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Video display modes step by step in the Ring programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Groovy programming language