Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to compare two float arrays for exact equality in Pascal

1 Answer

0 votes
{
    Compare two float arrays element-by-element for exact equality.
    Floating‑point values must match exactly; even tiny rounding
    differences will cause inequality. This is stricter than using
    a tolerance-based comparison.
}

program CompareFloatArraysExact;

{$mode objfpc}{$H+}

{
  Compares two float arrays and returns true only if all elements
  are exactly equal. Direct = comparison checks for exact binary equality.
}
function CompareFloatArraysExact(const A, B: array of Single): Boolean;
var
  i: Integer;
begin
  // If lengths differ, arrays cannot be equal
  if Length(A) <> Length(B) then
    Exit(False);

  // Compare each element directly
  for i := 0 to High(A) do
  begin
    if A[i] <> B[i] then
      Exit(False);  // Found mismatch → arrays differ
  end;

  // All elements matched exactly
  Result := True;
end;

{
  Prints the comparison result.
}
procedure PrintComparison(ResultValue: Boolean);
begin
  if ResultValue then
    WriteLn('Arrays are exactly equal.')
  else
    WriteLn('Arrays differ.');
end;

var
  floatArr1: array of Single;
  floatArr2: array of Single;
  resultValue: Boolean;

begin
  {
    Example arrays
  }
  floatArr1 := [12314.9872, 3.14, 12387.91834, 8873.579013];
  floatArr2 := [12314.9872, 3.14, 12387.91834, 8873.579013];

  // Perform exact comparison
  resultValue := CompareFloatArraysExact(floatArr1, floatArr2);

  // Output result
  PrintComparison(resultValue);
end.


{
run:

Arrays are exactly equal.

}

 



answered Sep 9 by avibootz
...