Approximate equality: Difference between revisions

m
→‎{{header|Phix}}: changed output format, undid 28/12/21 "fix" to printf()
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (→‎{{header|Phix}}: changed output format, undid 28/12/21 "fix" to printf())
Line 1,082:
It proved much harder to get decent-looking output than perform the tests, hence I allowed both the compare (cfmt) and display (dfmt) formats to be overridden.<br>
I got a different result for test 4 to everyone else, but simply setting the cfmt to "%.10f" got it the NOT.<br>
Likewise something similar for the trickier/ambiguous test 5, for which "0.000000" is as good as anything I can do, and both now show how to get either a true or false result.
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,091:
<span style="color: #000000;">da</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">db</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%30s isand\n%30s are %sapproximately equal to %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">da</span><span style="color: #0000FF;">,</span><span style="color: #000000;">eqsdb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dbeqs</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100000000000000.01</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000000000000.011</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.3f"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100.01</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100.011</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.3f"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000000000000.001</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10000.0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000000.0000001000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.10f"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.0010000001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.8f10f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- both</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.0010000001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.8f10f"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.10f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- ways</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.000000000000000000000101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- both</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.000000000000000000000101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%6f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- ways</span>
Line 1,108:
64 bit (implied by some of the accuracies specified for this task):
<pre>
100000000000000.010 is approximately equal to 100000000000000.011and
100100000000000000.01011 is NOTare approximately equal to 100.011
100.010 and
1000000000.0000001001 is approximately equal to 1000000000.0000001000
0.0010000000000 is approximately equal to 0 100.001000000100011 are NOT approximately equal
1000000000.0000001001 and
0.0010000000000 is NOT approximately equal to 0.001000000100
1000000000.00000010010000001000 isare approximately equal to 1000000000.0000001000
0.000000000000000000000101000 is NOT approximately equal to 0.000000
0.0010000000 and
0.000000000000000000000101000 is approximately equal to 0.000000
0.0010000001 2 isare approximately equal to 2
0.0010000000 -2 is approximately equal to -2and
0.00100000000000010000001 isare NOT approximately equal to 0.001000000100
3.14159265358979323851 is approximately equal to 3.14159265358979324003
0.000000 and
0.001000000000000000 isare NOT approximately equal to 0.001000000100
0.000000 and
0.000000 are approximately equal
2 and
2 are approximately equal
-2 and
-2 are approximately equal
3.14159265358979323851 and
3.14159265358979324003 are approximately equal
</pre>
32 bit (in fact a couple of them, the first and last pairs, are actually genuinely identical):
<pre>
100000000000000.016 is approximately equal to 100000000000000.016and
100100000000000000.01016 is NOTare approximately equal to 100.011
100.010 and
1000000000.0000002384 is approximately equal to 1000000000.0000001192
0.001000000000 is approximately equal to 0 100.001000000100011 are NOT approximately equal
1000000000.0000002384 and
0.001000000000 is NOT approximately equal to 0.001000000100
1000000000.00000023840000001192 isare approximately equal to 1000000000.0000001192
0.000000000000000000000101000 is NOT approximately equal to 0.000000
0.0010000000 and
0.000000000000000000000101000 is approximately equal to 0.000000
0.0010000001 2 isare approximately equal to 2
0.0010000000 -2 is approximately equal to -2and
0.0010000001 are NOT approximately equal
3.1415926535897931 is approximately equal to 3.1415926535897931
0.000000 and
0.000000 are NOT approximately equal
0.000000 and
0.000000 are approximately equal
2 and
2 are approximately equal
-2 and
-2 are approximately equal
3.1415926535897931 and
3.1415926535897931 isare approximately equal to 3.1415926535897931
</pre>
Note that %.8f prints 8 digits of precision on desktop/Phix, whereas the equivalent in
Javascript, as in number.toFixed(8), prints 8 decimal places.
 
=={{header|Python}}==
7,796

edits