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
<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;">"%.
<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;">"%.
<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
100.010 and
1000000000.0000001001 is approximately equal to 1000000000.0000001000▼
1000000000.0000001001 and
0.0010000000000 is NOT approximately equal to 0.001000000100▼
0.0010000000 and
0.0010000001
0.0010000000
0.000000 and
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
100.010 and
1000000000.0000002384 is approximately equal to 1000000000.0000001192▼
1000000000.0000002384 and
▲ 0.001000000000 is NOT approximately equal to 0.001000000100
0.0010000000 and
0.0010000001
0.0010000000
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
</pre>
=={{header|Python}}==
|