How to calculates the distance between two decimal numbers (absolute difference) in Pascal

1 Answer

0 votes
program DistanceBetweenNumbers;

uses
  Math;  // for Abs()

// Function that returns the distance between two decimal numbers
function DistanceBetween(a, b: Double): Double;
begin
  // The distance is the absolute value of the difference
  DistanceBetween := Abs(a - b);
end;

var
  x1, y1: Double;
  x2, y2: Double;
  x3, y3: Double;
  x4, y4: Double;

begin
  // Test values
  x1 := 100;   y1 := 45;
  x2 := 100;   y2 := -15;
  x3 := -100;  y3 := -125;
  x4 := -600;  y4 := 100;

  // Print results
  WriteLn('Distance between ', x1:0:2, ' and ', y1:0:2, ' = ', DistanceBetween(x1, y1):0:2);
  WriteLn('Distance between ', x2:0:2, ' and ', y2:0:2, ' = ', DistanceBetween(x2, y2):0:2);
  WriteLn('Distance between ', x3:0:2, ' and ', y3:0:2, ' = ', DistanceBetween(x3, y3):0:2);
  WriteLn('Distance between ', x4:0:2, ' and ', y4:0:2, ' = ', DistanceBetween(x4, y4):0:2);
end.



{
run:

Distance between 100.00 and 45.00 = 55.00
Distance between 100.00 and -15.00 = 115.00
Distance between -100.00 and -125.00 = 25.00
Distance between -600.00 and 100.00 = 700.00

}

 



answered Jun 28 by avibootz

Related questions

...