RosettaCodeData/Task/Approximate-equality/J/approximate-equality.j

45 lines
1.5 KiB
Plaintext

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