Search code examples
pascalfreepascalturbo-pascal

Troubles with pascal conditions


I'm writing the program with Pascal. And have some troubles with conditions.

For example, if you write in input
1 1
1 4
5 1
2 2

tArea1 = 6 and sumAreas = 6 too
But in "if" structure this doesn't working correctly.

Help me, please. Tnx.

var
  x1,x2,x3,x4,y1,y2,y3,y4: real;
  line1Length, line2Length, line3Length : real; // Length of the triangle area
  tArea1, tArea2, tArea3, tArea4, sumAreas : real;

  function segmentLength (x,y,x0,y0:real);
  begin
    segmentLength := sqrt(sqr(x-x0) + sqr(y-y0));
  end;

  function triangleArea (a,b,c:real);
  var
    p: real; // Half of the perimetr
  begin
    p := (a+b+c)/2;
    triangleArea := sqrt(p*(p-a)*(p-b)*(p-c));
  end;

begin
  writeln('write x1,y1');
  readln(x1,y1);
  writeln('write x2,y2');
  readln(x2,y2);
  writeln('write x3,y3');
  readln(x3,y3);
  writeln('write x4,y4');
  readln(x4,y4);

  // First triangle
  line1Length := segmentLength(x1,y1,x2,y2);
  line2Length := segmentLength(x2,y2,x3,y3);
  line3Length := segmentLength(x3,y3,x1,y1);

  tArea1 := triangleArea(line1Length, line2Length, line3Length);

  // Second triangle
  line1Length := segmentLength(x4,y4,x2,y2);
  line2Length := segmentLength(x2,y2,x3,y3);
  line3Length := segmentLength(x3,y3,x4,y4);

  tArea2 := triangleArea(line1Length, line2Length, line3Length);

  // Third triangle

  line1Length := segmentLength(x4,y4,x1,y1);
  line2Length := segmentLength(x1,y1,x3,y3);
  line3Length := segmentLength(x3,y3,x4,y4);

  tArea3 := triangleArea(line1Length, line2Length, line3Length);

  // Fourth Triangle

  line1Length := segmentLength(x4,y4,x1,y1);
  line2Length := segmentLength(x1,y1,x2,y2);
  line3Length := segmentLength(x2,y2,x4,y4);

  tArea4 := triangleArea(line1Length, line2Length, line3Length);

  // Check dot situated

  sumAreas := tArea4+tArea2+tArea3;

  writeln(tArea1, ' // ', sumAreas); //

  if (sumAreas = tArea1) then
  begin
    writeln('In');
  end
  else
  begin
    writeln('Out');
  end;

end.

Solution

  • Because of the way floating point numbers are represented by the computer there can be inconsistencies when comparing two numbers that appear to be identical. Unlike integers, IEEE floating point numbers are only approximates, not exact numbers. The need to convert the numbers to a form the computer can store in binary leads to minor precision or round-off deviations. For example 1.3 may really be represented as 1.29999999999.

    Therefore you should never use = or <> to compare two floating point numbers. Instead, subtract the two numbers and compare them against a very small number.

    For you case try to use:

     if abs(sumAreas - tArea1) < 0.00001 then
    

    There may particularly be problems when using conversion functions such as StrToFloat, TextToFloat and StrToCurr

     if FloatToStr(sumAreas) = FloatToStr(tArea1) then
    

    Also it, but not recommended:

    if Round(sumAreas) = Round(tArea1) then
    

    Reference: Problems comparing floating point numbers.