WeixiangYan/CodeTransOcean huggingface dataset:
Task: Find the missing permutation







Python: from itertools import permutations

given = .split()

allPerms = [''.join(x) for x in permutations(given[0])]

missing = list(set(allPerms) - set(given)) 




Fortran: program missing_permutation

  implicit none
  character (4), dimension (23), parameter :: list =                    &
    & (/'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', &
    &   'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA', &
    &   'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'/)
  integer :: i, j, k

  do i = 1, 4
    j = minloc ((/(count (list (:) (i : i) == list (1) (k : k)), k = 1, 4)/), 1)
    write (*, '(a)', advance = 'no') list (1) (j : j)
  end do
  write (*, *)

end program missing_permutation
Task: Find the missing permutation








VB: Dim PList as QStringList
PList.addItems "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB"
PList.additems "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA"
PList.additems "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"

Dim NumChar(4, 65 to 68) as integer
Dim MPerm as string


For x = 0 to PList.Itemcount -1
    for y = 1 to 4
        Inc(NumChar(y, asc(PList.Item(x)[y])))
    next
next


for x = 1 to 4
    for y = 65 to 68
        MPerm = MPerm + iif(NumChar(x, y)=5, chr$(y), "")
    next
next

showmessage MPerm 




Fortran: program missing_permutation

  implicit none
  character (4), dimension (23), parameter :: list =                    &
    & (/'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', &
    &   'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA', &
    &   'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'/)
  integer :: i, j, k

  do i = 1, 4
    j = minloc ((/(count (list (:) (i : i) == list (1) (k : k)), k = 1, 4)/), 1)
    write (*, '(a)', advance = 'no') list (1) (j : j)
  end do
  write (*, *)

end program missing_permutation
Task: Find the missing permutation






PHP: <?php
$finalres = Array();
function permut($arr,$result=array()){
	global  $finalres;
	if(empty($arr)){
		$finalres[] = implode("",$result);
	}else{
		foreach($arr as $key => $val){
			$newArr = $arr;
			$newres = $result;
			$newres[] = $val;
			unset($newArr[$key]);
			permut($newArr,$newres);		
		}
	}
}
$givenPerms = Array("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
$given = Array("A","B","C","D");
permut($given);
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )





Fortran: program missing_permutation

  implicit none
  character (4), dimension (23), parameter :: list =                    &
    & (/'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB', &
    &   'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA', &
    &   'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'/)
  integer :: i, j, k

  do i = 1, 4
    j = minloc ((/(count (list (:) (i : i) == list (1) (k : k)), k = 1, 4)/), 1)
    write (*, '(a)', advance = 'no') list (1) (j : j)
  end do
  write (*, *)

end program missing_permutation
Task: Find the missing permutation






PHP: <?php
$finalres = Array();
function permut($arr,$result=array()){
	global  $finalres;
	if(empty($arr)){
		$finalres[] = implode("",$result);
	}else{
		foreach($arr as $key => $val){
			$newArr = $arr;
			$newres = $result;
			$newres[] = $val;
			unset($newArr[$key]);
			permut($newArr,$newres);		
		}
	}
}
$givenPerms = Array("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
$given = Array("A","B","C","D");
permut($given);
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )
Task: Find the missing permutation






PHP: <?php
$finalres = Array();
function permut($arr,$result=array()){
	global  $finalres;
	if(empty($arr)){
		$finalres[] = implode("",$result);
	}else{
		foreach($arr as $key => $val){
			$newArr = $arr;
			$newres = $result;
			$newres[] = $val;
			unset($newArr[$key]);
			permut($newArr,$newres);		
		}
	}
}
$givenPerms = Array("ABCD","CABD","ACDB","DACB","BCDA","ACBD","ADCB","CDAB","DABC","BCAD","CADB","CDBA","CBAD","ABDC","ADBC","BDCA","DCBA","BACD","BADC","BDAC","CBDA","DBCA","DCAB");
$given = Array("A","B","C","D");
permut($given);
print_r(array_diff($finalres,$givenPerms)); // Array ( [20] => DBAC )






Ada: with Ada.Text_IO;
procedure Missing_Permutations is
   subtype Permutation_Character is Character range 'A' .. 'D';

   Character_Count : constant :=
      1 + Permutation_Character'Pos (Permutation_Character'Last)
        - Permutation_Character'Pos (Permutation_Character'First);

   type Permutation_String is
     array (1 .. Character_Count) of Permutation_Character;

   procedure Put (Item : Permutation_String) is
   begin
      for I in Item'Range loop
         Ada.Text_IO.Put (Item (I));
      end loop;
   end Put;

   Given_Permutations : array (Positive range <>) of Permutation_String :=
     ("ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
      "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
      "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD",
      "BADC", "BDAC", "CBDA", "DBCA", "DCAB");

   Count     : array (Permutation_Character, 1 .. Character_Count) of Natural
      := (others => (others => 0));
   Max_Count : Positive := 1;

   Missing_Permutation : Permutation_String;
begin
   for I in Given_Permutations'Range loop
      for Pos in 1 .. Character_Count loop
         Count (Given_Permutations (I) (Pos), Pos)   :=
           Count (Given_Permutations (I) (Pos), Pos) + 1;
         if Count (Given_Permutations (I) (Pos), Pos) > Max_Count then
            Max_Count := Count (Given_Permutations (I) (Pos), Pos);
         end if;
      end loop;
   end loop;

   for Char in Permutation_Character loop
      for Pos in 1 .. Character_Count loop
         if Count (Char, Pos) < Max_Count then
            Missing_Permutation (Pos) := Char;
         end if;
      end loop;
   end loop;

   Ada.Text_IO.Put_Line ("Missing Permutation:");
   Put (Missing_Permutation);
end Missing_Permutations;
Task: Approximate equality



C#: using System;

public static class Program
{
    public static void Main() {
        Test(100000000000000.01, 100000000000000.011);
        Test(100.01, 100.011);
        Test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        Test(0.001, 0.0010000001);
        Test(0.000000000000000000000101, 0.0);
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0);
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0);
        Test(3.14159265358979323846, 3.14159265358979324);

        void Test(double a, double b) {
            const double epsilon = 1e-18;
            WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}");
        }
    }

    public static bool ApproxEquals(this double value, double other, double epsilon) => Math.Abs(value - other) < epsilon;
}









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
      
      
      
      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;
Task: Approximate equality

C: #include <math.h>
#include <stdbool.h>
#include <stdio.h>

bool approxEquals(double value, double other, double epsilon) {
    return fabs(value - other) < epsilon;
}

void test(double a, double b) {
    double epsilon = 1e-18;
    printf("%f, %f => %d\n", a, b, approxEquals(a, b, epsilon));
}

int main() {
    test(100000000000000.01, 100000000000000.011);
    test(100.01, 100.011);
    test(10000000000000.001 / 10000.0, 1000000000.0000001000);
    test(0.001, 0.0010000001);
    test(0.000000000000000000000101, 0.0);
    test(sqrt(2.0) * sqrt(2.0), 2.0);
    test(-sqrt(2.0) * sqrt(2.0), -2.0);
    test(3.14159265358979323846, 3.14159265358979324);
    return 0;
}











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
      
      
      
      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;
Task: Approximate equality


C++: #include <iomanip>
#include <iostream>
#include <cmath>

bool approxEquals(double a, double b, double e) {
    return fabs(a - b) < e;
}

void test(double a, double b) {
    constexpr double epsilon = 1e-18;
    std::cout << std::setprecision(21) << a;
    std::cout << ", ";
    std::cout << std::setprecision(21) << b;
    std::cout << " => ";
    std::cout << approxEquals(a, b, epsilon) << '\n';
}

int main() {
    test(100000000000000.01, 100000000000000.011);
    test(100.01, 100.011);
    test(10000000000000.001 / 10000.0, 1000000000.0000001000);
    test(0.001, 0.0010000001);
    test(0.000000000000000000000101, 0.0);
    test(sqrt(2.0) * sqrt(2.0), 2.0);
    test(-sqrt(2.0) * sqrt(2.0), -2.0);
    test(3.14159265358979323846, 3.14159265358979324);
    return 0;
}










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
      
      
      
      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;
Task: Approximate equality




Go: package main

import (
    "fmt"
    "log"
    "math/big"
)

func max(a, b *big.Float) *big.Float {
    if a.Cmp(b) > 0 {
        return a
    }
    return b
}

func isClose(a, b *big.Float) bool {
    relTol := big.NewFloat(1e-9) 
    t := new(big.Float)
    t.Sub(a, b)
    t.Abs(t)
    u, v, w := new(big.Float), new(big.Float), new(big.Float)
    u.Mul(relTol, max(v.Abs(a), w.Abs(b)))
    return t.Cmp(u) <= 0
}

func nbf(s string) *big.Float {
    n, ok := new(big.Float).SetString(s)
    if !ok {
        log.Fatal("invalid floating point number")
    }
    return n
}

func main() {
    root2 := big.NewFloat(2.0)
    root2.Sqrt(root2)
    pairs := [][2]*big.Float{
        {nbf("100000000000000.01"), nbf("100000000000000.011")},
        {nbf("100.01"), nbf("100.011")},
        {nbf("0").Quo(nbf("10000000000000.001"), nbf("10000.0")), nbf("1000000000.0000001000")},
        {nbf("0.001"), nbf("0.0010000001")},
        {nbf("0.000000000000000000000101"), nbf("0.0")},
        {nbf("0").Mul(root2, root2), nbf("2.0")},
        {nbf("0").Mul(nbf("0").Neg(root2), root2), nbf("-2.0")},
        {nbf("100000000000000003.0"), nbf("100000000000000004.0")},
        {nbf("3.14159265358979323846"), nbf("3.14159265358979324")},
    }
    for _, pair := range pairs {
        s := "≉"
        if isClose(pair[0], pair[1]) {
            s = "≈"
        }
        fmt.Printf("% 21.19g %s %- 21.19g\n", pair[0], s, pair[1])
    }
}








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
      
      
      
      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;
Task: Approximate equality





Java: public class Approximate {
    private static boolean approxEquals(double value, double other, double epsilon) {
        return Math.abs(value - other) < epsilon;
    }

    private static void test(double a, double b) {
        double epsilon = 1e-18;
        System.out.printf("%f, %f => %s\n", a, b, approxEquals(a, b, epsilon));
    }

    public static void main(String[] args) {
        test(100000000000000.01, 100000000000000.011);
        test(100.01, 100.011);
        test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        test(0.001, 0.0010000001);
        test(0.000000000000000000000101, 0.0);
        test(Math.sqrt(2.0) * Math.sqrt(2.0), 2.0);
        test(-Math.sqrt(2.0) * Math.sqrt(2.0), -2.0);
        test(3.14159265358979323846, 3.14159265358979324);
    }
}







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
      
      
      
      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;
Task: Approximate equality







Python: from numpy import sqrt
from math import isclose

testvalues = [[100000000000000.01,           100000000000000.011],
              [100.01,                       100.011],
              [10000000000000.001 / 10000.0, 1000000000.0000001000],
              [0.001,                        0.0010000001],
              [0.000000000000000000000101,   0.0],
              [sqrt(2) * sqrt(2),            2.0],
              [-sqrt(2) * sqrt(2),          -2.0],
              [3.14159265358979323846,       3.14159265358979324]]

for (x, y) in testvalues:
    maybenot = "is" if isclose(x, y) else "is NOT"
    print(x, maybenot, "approximately equal to ", y)





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
      
      
      
      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;
Task: Approximate equality








VB: Imports System.Runtime.CompilerServices

Module Module1

    <Extension()>
    Function ApproxEquals(ByVal value As Double, other As Double, epsilon As Double)
        Return Math.Abs(value - other) < epsilon
    End Function

    Sub Test(a As Double, b As Double)
        Dim epsilon = 1.0E-18
        Console.WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}")
    End Sub

    Sub Main()
        Test(100000000000000.02, 100000000000000.02)
        Test(100.01, 100.011)
        Test(10000000000000.002 / 10000.0, 1000000000.0000001)
        Test(0.001, 0.0010000001)
        Test(1.01E-22, 0.0)
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0)
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0)
        Test(3.1415926535897931, 3.1415926535897931)
    End Sub

End Module




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
      
      
      
      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;
Task: Approximate equality

C: #include <math.h>
#include <stdbool.h>
#include <stdio.h>

bool approxEquals(double value, double other, double epsilon) {
    return fabs(value - other) < epsilon;
}

void test(double a, double b) {
    double epsilon = 1e-18;
    printf("%f, %f => %d\n", a, b, approxEquals(a, b, epsilon));
}

int main() {
    test(100000000000000.01, 100000000000000.011);
    test(100.01, 100.011);
    test(10000000000000.001 / 10000.0, 1000000000.0000001000);
    test(0.001, 0.0010000001);
    test(0.000000000000000000000101, 0.0);
    test(sqrt(2.0) * sqrt(2.0), 2.0);
    test(-sqrt(2.0) * sqrt(2.0), -2.0);
    test(3.14159265358979323846, 3.14159265358979324);
    return 0;
}
Task: Approximate equality



C#: using System;

public static class Program
{
    public static void Main() {
        Test(100000000000000.01, 100000000000000.011);
        Test(100.01, 100.011);
        Test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        Test(0.001, 0.0010000001);
        Test(0.000000000000000000000101, 0.0);
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0);
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0);
        Test(3.14159265358979323846, 3.14159265358979324);

        void Test(double a, double b) {
            const double epsilon = 1e-18;
            WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}");
        }
    }

    public static bool ApproxEquals(this double value, double other, double epsilon) => Math.Abs(value - other) < epsilon;
}