Approximate equality: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 27:
* -sqrt(2) * sqrt(2), -2.0
* 3.14159265358979323846, 3.14159265358979324
 
=={{header|Python}}==
<lang python>from numpy import sqrt
from math import isclose
 
testvalues = [[100000000000000.01,100000000000000.011], [00.01, 100.011],
[10000000000000.001 / 10000.0, 1000000000.0000001000],
[0.001, 0.0010000001], [0.00000000000000000101, 0.0],
[sqrt(2) * sqrt(2), 2.0], [-sqrt(2) * sqrt(2), -2.0],
[100000000000000003.0, 100000000000000004.0],
[3.14159265358979323846, 3.14159265358979324]]
 
for (x, y) in testvalues:
maybenot = "" if isclose(x, y) else "NOT"
print(x, "is", maybenot, "approximately equal to ", y)
 
</lang>{{out}}
<pre>
100000000000000.02 is approximately equal to 100000000000000.02
0.01 is NOT approximately equal to 100.011
1000000000.0000002 is approximately equal to 1000000000.0000001
0.001 is NOT approximately equal to 0.0010000001
1.01e-18 is NOT approximately equal to 0.0
2.0 is approximately equal to 2.0
-2.0 is approximately equal to -2.0
1e+17 is approximately equal to 1e+17
3.141592653589793 is approximately equal to 3.141592653589793
</pre>
4,105

edits