Approximate equality: Difference between revisions

Line 364:
 
=={{header|J}}==
Attributed to Ken Iverson, inventor of APL and of course his final dialect, j,
"In an early talk Ken was explaining the advantages of tolerant comparison. A member of the audience asked incredulously, “Surely you don’t mean that when A=B and B=C, A may not equal C?” Without skipping a beat, Ken replied, “Any carpenter knows that!” and went on to the next question."
 
J includes a "customization" conjunction ( !. ) that delivers variants of some verbs. Comparisons are tolerant by default, and their tolerance can be customized to some level. Specifying =!.0 specifies "no tolerance". Specifying a tolerance of 1e_8 is a domain error because that's no longer math. Write your own verb if you need this.
<lang>
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
</lang>
 
=={{header|Java}}==
{{trans|Kotlin}}
Anonymous user