Approximate equality: Difference between revisions

Content added Content deleted
No edit summary
Line 213: Line 213:
-2.000000000000000000, -2.000000000000000000 => true
-2.000000000000000000, -2.000000000000000000 => true
3.141592653589793116, 3.141592653589793116 => true</pre>
3.141592653589793116, 3.141592653589793116 => true</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Math}}
The Delphi has a Math.SameValue function for compare, but all float operations use by default Extended (High precision), we need use double cast for every operation, like division, multiply and square tree.
<lang Delphi>
program Approximate_Equality;

{$APPTYPE CONSOLE}

uses
System.SysUtils,
System.Math;

const
EPSILON: Double = 1E-18;

procedure Test(a, b: Double; Expected: Boolean);
var
result: Boolean;
const
STATUS: array[Boolean] of string = ('FAIL', 'OK');
begin
result := SameValue(a, b, EPSILON);
Write(a, ' ', b, ' => ', result, ' '^I);
writeln(Expected, ^I, STATUS[Expected = result]);
end;

begin
Test(100000000000000.01, 100000000000000.011, True);
Test(100.01, 100.011, False);
Test(double(10000000000000.001) / double(10000.0), double(1000000000.0000001000),
False);
Test(0.001, 0.0010000001, False);
Test(0.000000000000000000000101, 0.0, True);
Test(double(Sqrt(2)) * double(Sqrt(2)), 2.0, False);
Test(-double(Sqrt(2)) * double(Sqrt(2)), -2.0, false);
Test(3.14159265358979323846, 3.14159265358979324, True);
Readln;
end.

</lang>

{{out}}
<pre>
1.00000000000000E+0014 1.00000000000000E+0014 => TRUE TRUE OK
1.00010000000000E+0002 1.00011000000000E+0002 => FALSE FALSE OK
1.00000000000000E+0009 1.00000000000000E+0009 => FALSE FALSE OK
1.00000000000000E-0003 1.00000010000000E-0003 => FALSE FALSE OK
1.01000000000000E-0022 0.00000000000000E+0000 => TRUE TRUE OK
2.00000000000000E+0000 2.00000000000000E+0000 => FALSE FALSE OK
-2.00000000000000E+0000 -2.00000000000000E+0000 => FALSE FALSE OK
3.14159265358979E+0000 3.14159265358979E+0000 => TRUE TRUE OK
</pre>


=={{header|Factor}}==
=={{header|Factor}}==