How to resolve the algorithm Approximate equality step by step in the jq programming language
How to resolve the algorithm Approximate equality step by step in the jq 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 jq programming language
Source code in the jq programming language
# Return whether the two numbers `a` and `b` are close.
# Closeness is determined by the `epsilon` parameter -
# the numbers are considered close if the difference between them
# is no more than epsilon * max(abs(a), abs(b)).
def isclose(a; b; epsilon):
((a - b) | fabs) <= (([(a|fabs), (b|fabs)] | max) * epsilon);
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
def lpad: lpad(20; " ");
# test values
def tv: [
{x: 100000000000000.01, y: 100000000000000.011 },
{x: 100.01, y: 100.011 },
{x: (10000000000000.001 / 10000.0), y: 1000000000.0000001000 },
{x: 0.001, y: 0.0010000001 },
{x: 0.000000000000000000000101, y: 0.0 },
{x: ((2|sqrt) * (2|sqrt)), y: 2.0 },
{x: (-(2|sqrt) * (2|sqrt)), y: -2.0 },
{x: 3.14159265358979323846, y: 3.14159265358979324 }
]
;
tv[] | "\(.x|lpad) \(if isclose(.x; .y; 1.0e-9) then " ≈ " else " ≉ " end) \(.y|lpad)"
100000000000000.02 ≈ 100000000000000.02
100.01 ≉ 100.011
1000000000.0000002 ≈ 1000000000.0000001
0.001 ≉ 0.0010000001
1.01e-22 ≉ 0
2.0000000000000004 ≈ 2
-2.0000000000000004 ≈ -2
3.141592653589793 ≈ 3.141592653589793
You may also check:How to resolve the algorithm Chaocipher step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the C programming language
You may also check:How to resolve the algorithm RIPEMD-160 step by step in the C programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Scheme programming language