Approximate equality: Difference between revisions

Content added Content deleted
(Approximate equality in FreeBASIC)
No edit summary
Line 32: Line 32:


__TOC__
__TOC__

=={{header|Ada}}==
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;

procedure Main is
type Real is digits 18;
package Real_Funcs is new Ada.Numerics.Generic_Elementary_Functions(Real);
use Real_Funcs;
package Real_IO is new Ada.Text_IO.Float_IO(Real);
use Real_IO;
function Approx_Equal (Left : Real; Right : Real) return Boolean is
-- Calculate an epsilon value based upon the magnitude of the
-- maximum value of the two parameters
eps : Real := Real'Max(Left, Right) * 1.0e-9;
begin
if left > Right then
return Left - Right < eps;
else
return Right - Left < eps;
end if;
end Approx_Equal;
Type Index is (Left, Right);
type Pairs_List is array (Index) of Real;
type Pairs_Table is array(1..8) of Pairs_List;
Table : Pairs_Table;
begin
Table := ((100000000000000.01, 100000000000000.011),
(100.01, 100.011),
(10000000000000.001 / 10000.0, 1000000000.0000001000),
(0.001, 0.0010000001),
(0.000000000000000000000101, 0.0),
(sqrt(2.0) * sqrt(2.0), 2.0),
(-sqrt(2.0) * sqrt(2.0), -2.0),
(3.14159265358979323846, 3.14159265358979324));

for Pair of Table loop
Put(Item => Pair(Left), Exp => 0, Aft => 16, Fore => 6);
Put(" ");
Put(Item => Pair(Right), Exp => 0, Aft => 16, Fore => 6);
Put_Line(" " & Boolean'Image(Approx_Equal(Pair(Left), Pair(Right))));
end loop;
end Main;
</lang>
{{Output}}
<pre>
100000000000000.0100000000000000 100000000000000.0110000000000000 TRUE
100.0100000000000000 100.0110000000000000 FALSE
1000000000.0000001000000000 1000000000.0000001000000000 TRUE
0.0010000000000000 0.0010000001000000 FALSE
0.0000000000000000 0.0000000000000000 FALSE
2.0000000000000000 2.0000000000000000 TRUE
-2.0000000000000000 -2.0000000000000000 FALSE
3.1415926535897932 3.1415926535897932 TRUE
</pre>


=={{header|AWK}}==
=={{header|AWK}}==