Approximate equality: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
|||
Line 1,079: | Line 1,079: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Traditionally I have always just used sprintf() to compare floating point atoms in phix.<br> |
Traditionally I have always just used sprintf() to compare floating point atoms in phix.<br> |
||
This task (imo) is trying to make a general-purpose routine out of code |
This task (imo) is trying to make a general-purpose routine out of code which is best tailored for each and every specific task.<br> |
||
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> |
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 "%. |
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, and both now show how to get either a true or false result. |
Likewise something similar for the trickier/ambiguous test 5, and both now show how to get either a true or false result. |
||
<!--<lang Phix>(phixonline)--> |
|||
<lang Phix>procedure test(atom a,b, string dfmt="%g", cfmt="%g") |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
bool eq = sprintf(cfmt,a)==sprintf(cfmt,b) |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">dfmt</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"%g"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cfmt</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"%g"</span><span style="color: #0000FF;">)</span> |
|||
string eqs = iff(eq?"":"NOT "), |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">ca</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span> |
|||
sa = sprintf(dfmt,a), |
|||
<span style="color: #000000;">cb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span> |
|||
sb = sprintf(dfmt,b) |
|||
<span style="color: #000000;">eqs</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ca</span><span style="color: #0000FF;">=</span><span style="color: #000000;">cb</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"NOT "</span><span style="color: #0000FF;">),</span> |
|||
printf(1,"%30s is %sapproximately equal to %s\n",{sa,eqs,sb}) |
|||
<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> |
|||
end procedure |
|||
<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 is %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;">eqs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">db</span><span style="color: #0000FF;">})</span> |
|||
test(100000000000000.01,100000000000000.011,"%.3f") |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
test(100.01,100.011) |
|||
test(10000000000000.001/10000.0,1000000000.0000001000,"%.10f") |
|||
<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> |
|||
test(0.001,0.0010000001,"%.8f") -- both |
|||
<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> |
|||
test(0.001,0.0010000001,"%.8f","%.8f") -- ways |
|||
<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> |
|||
test(0.000000000000000000000101,0.0,"%f") -- both |
|||
<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;">"%.8f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- both</span> |
|||
test(0.000000000000000000000101,0.0,"%f","%6f") -- ways |
|||
<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;">"%.8f"</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> |
|||
test(sqrt(2)*sqrt(2),2.0) |
|||
<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> |
|||
test(-sqrt(2)*sqrt(2),-2.0) |
|||
<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> |
|||
test(3.14159265358979323846,3.14159265358979324,"%.20f")</lang> |
|||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2.0</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(-</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),-</span><span style="color: #000000;">2.0</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3.14159265358979323846</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3.14159265358979324</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.20f"</span><span style="color: #0000FF;">)</span> |
|||
<!--</lang>--> |
|||
{{out}} |
{{out}} |
||
64 bit (implied by some of the accuracies specified for this task): |
64 bit (implied by some of the accuracies specified for this task): |
||
Line 1,107: | Line 1,111: | ||
100.01 is NOT approximately equal to 100.011 |
100.01 is NOT approximately equal to 100.011 |
||
1000000000.0000001001 is approximately equal to 1000000000.0000001000 |
1000000000.0000001001 is approximately equal to 1000000000.0000001000 |
||
0.0010000000000 is approximately equal to 0.001000000100 |
|||
0.0010000000000 is NOT approximately equal to 0.001000000100 |
|||
0.000000000000000000000101000 is NOT approximately equal to 0.000000 |
0.000000000000000000000101000 is NOT approximately equal to 0.000000 |
||
0.000000000000000000000101000 is approximately equal to 0.000000 |
0.000000000000000000000101000 is approximately equal to 0.000000 |
||
Line 1,120: | Line 1,124: | ||
100.01 is NOT approximately equal to 100.011 |
100.01 is NOT approximately equal to 100.011 |
||
1000000000.0000002384 is approximately equal to 1000000000.0000001192 |
1000000000.0000002384 is approximately equal to 1000000000.0000001192 |
||
0.001000000000 is approximately equal to 0.001000000100 |
|||
0.001000000000 is NOT approximately equal to 0.001000000100 |
|||
0.000000000000000000000101000 is NOT approximately equal to 0.000000 |
0.000000000000000000000101000 is NOT approximately equal to 0.000000 |
||
0.000000000000000000000101000 is approximately equal to 0.000000 |
0.000000000000000000000101000 is approximately equal to 0.000000 |
||
Line 1,128: | Line 1,132: | ||
3.1415926535897931 is approximately equal to 3.1415926535897931 |
3.1415926535897931 is approximately equal to 3.1415926535897931 |
||
</pre> |
</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}}== |
=={{header|Python}}== |