How to resolve the algorithm Approximate equality step by step in the J programming language
How to resolve the algorithm Approximate equality step by step in the J 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 J programming language
Source code in the j programming language
NB. default comparison tolerance matches the python result
".;._2]0 :0
100000000000000.01 = 100000000000000.011
100.01 = 100.011
(10000000000000.001 % 10000.0) = 1000000000.0000001000
0.001 = 0.0010000001
0.000000000000000000000101 = 0.0
(= ([: *~ %:)) 2 NB. sqrt(2)*sqrt(2)
((= -)~ ([: (* -) %:)) 2 NB. -sqrt(2) * sqrt(2), -2.0
3.14159265358979323846 = 3.14159265358979324
)
1 0 1 0 0 1 1 1
NB. tolerance of 1e_12 matches the python result
".;._2]0 :0[CT=:1e_12
100000000000000.01 =!.CT 100000000000000.011
100.01 =!.CT 100.011
(10000000000000.001 % 10000.0) =!.CT 1000000000.0000001000
0.001 =!.CT 0.0010000001
0.000000000000000000000101 =!.CT 0.0
(=!.CT ([: *~ %:)) 2 NB. sqrt(2)*sqrt(2)
((=!.CT -)~ ([: (* -) %:)) 2 NB. -sqrt(2) * sqrt(2), -2.0
3.14159265358979323846 =!.CT 3.14159265358979324
)
1 0 1 0 0 1 1 1
NB. tight tolerance
".;._2]0 :0[CT=:1e_18
100000000000000.01 =!.CT 100000000000000.011
100.01 =!.CT 100.011
(10000000000000.001 % 10000.0) =!.CT 1000000000.0000001000
0.001 =!.CT 0.0010000001
0.000000000000000000000101 =!.CT 0.0
(=!.CT ([: *~ %:)) 2 NB. sqrt(2)*sqrt(2)
((=!.CT -)~ ([: (* -) %:)) 2 NB. -sqrt(2) * sqrt(2), -2.0
3.14159265358979323846 =!.CT 3.14159265358979324
)
1 0 0 0 0 0 0 1
2 (=!.1e_8) 9
|domain error
| 2(= !.1e_8)9
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Deconvolution/1D step by step in the C programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Sidef programming language
You may also check:How to resolve the algorithm Pi step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the Seed7 programming language