Approximate equality: Difference between revisions

(added ReScript)
Line 1,046:
;;
</lang>
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
The constants <tt>minReal</tt>, <tt>maxReal</tt> and <tt>epsReal</tt> are defined by the ISO standard 10206 (“Extended Pascal”).
However, their specific values are “implementation defined”, i.&nbsp;e. it is up to the compiler vendors to assign concrete values to them.
<lang pascal>program approximateEqual(output);
 
{
\brief determines whether two `real` values are approximately equal
\param x a reference value
\param y the value to compare with \param x
\return true if \param x is equal or approximately equal to \param y
}
function equal(protected x, y: real): Boolean;
function approximate: Boolean;
function d(protected x: real): integer;
begin
d := trunc(ln(abs(x) + minReal) / ln(2)) + 1
end;
begin
approximate := abs(x - y) <= epsReal * (maxReal / (d(x) + d(y)))
end;
begin
equal := (x = y) or_else (x * y >= 0.0) and_then approximate
end;
 
{ --- auxilliary routines ---------------------------------------------- }
procedure test(protected x, y: real);
const
{ ANSI escape code for color coding }
CSI = chr(8#33) + '[';
totalMinimumWidth = 40;
postRadixDigits = 24;
begin
write(x:totalMinimumWidth:postRadixDigits, '':1, CSI, '1;3');
if equal(x, y) then
begin
if x = y then
begin
write('2m≅')
end
else
begin
write('5m≆')
end
end
else
begin
write('1m≇')
end;
writeLn(CSI, 'm', '':1, y:totalMinimumWidth:postRadixDigits)
end;
 
{ === MAIN ============================================================= }
var
n: integer;
x: real;
begin
{ Variables were used to thwart compile-time evaluation done }
{ by /some/ compilers potentially confounding the results. }
n := 2;
x := 100000000000000.01;
test(x, 100000000000000.011);
test(100.01, 100.011);
test(x / 10000.0, 1000000000.0000001000);
test(0.001, 0.0010000001);
test(0.000000000000000000000101, 0.0);
x := sqrt(n);
test(sqr(x), 2.0);
test((-x) * x, -2.0);
test(3.14159265358979323846, 3.14159265358979324)
end.</lang>
{{out}}
100000000000000.015625000000000000000000 ≅ 100000000000000.015625000000000000000000
100.010000000000005115907697 ≆ 100.010999999999995679900167
10000000000.000001907348632812500000 ≆ 1000000000.000000119209289550781250
0.001000000000000000020817 ≇ 0.001000000100000000054917
0.000000000000000000000101 ≇ 0.000000000000000000000000
2.000000000000000444089210 ≆ 2.000000000000000000000000
-2.000000000000000444089210 ≆ -2.000000000000000000000000
3.141592653589793115997963 ≅ 3.141592653589793115997963
The shown output was generated by a <tt>program</tt> compiled by the GPC (GNU Pascal Compiler).
Due to technical limitations it was not possible to reproduce 100.01&nbsp;≆nbsp;100.011 as requested by the task specification.
The computer had a IEEE-754-compliant FPU with 80-bit precision.
Note that Pascal’s <tt>write</tt>/<tt>writeLn</tt>/<tt>writeStr</tt> routines (the last one is only available in Extended Pascal) produce _rounded_ representations.
 
=={{header|Perl}}==
149

edits