Pascal 帕斯卡条件的问题

Pascal 帕斯卡条件的问题,pascal,freepascal,turbo-pascal,Pascal,Freepascal,Turbo Pascal,我正在用Pascal编写程序。还有一些条件方面的问题 例如,如果您在输入中写入 11 14 5.1 2 tArea1=6和sumAreas=6也 但在“如果”结构中,这不能正常工作 请帮帮我。Tnx var x1,x2,x3,x4,y1,y2,y3,y4: real; line1Length, line2Length, line3Length : real; // Length of the triangle area tArea1, tArea2, tArea3, tArea4,

我正在用Pascal编写程序。还有一些条件方面的问题

例如,如果您在输入中写入
11
14
5.1
2

tArea1
=6和
sumAreas
=6也
但在“如果”结构中,这不能正常工作

请帮帮我。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.

您正在将浮点值与相等(=)运算符进行比较。比较浮点值时,值的微小差异(由于数字原因)可能会导致偏差,导致比较失败

更好的平等测试是

 if abs(value-valuetocompareto)<eps then
   writeln('bingo') 
 else
   writeln('tryagain');

如果abs(value-valuetocompareto)您将浮点值与相等(=)运算符进行比较。比较浮点值时,值的微小差异(由于数字原因)可能会导致偏差,导致比较失败

更好的平等测试是

 if abs(value-valuetocompareto)<eps then
   writeln('bingo') 
 else
   writeln('tryagain');

如果abs(value-valuetocompareto)由于计算机表示浮点数的方式,比较两个看起来相同的数字时可能会出现不一致。与整数不同,IEEE浮点数只是近似数,而不是精确数。需要将数字转换成计算机可以存储在二进制中的形式,这会导致较小的精度或舍入偏差。例如,
1.3
实际上可能表示为
1.299999999

因此,永远不要使用
=
来比较两个浮点数。相反,将这两个数字相减,并将它们与一个非常小的数字进行比较

对于您的情况,请尝试使用:

 if abs(sumAreas - tArea1) < 0.00001 then
此外,还建议,但不建议:

if Round(sumAreas) = Round(tArea1) then

参考资料:

由于计算机表示浮点数的方式,在比较两个看似相同的数字时可能会出现不一致。与整数不同,IEEE浮点数只是近似数,而不是精确数。需要将数字转换成计算机可以存储在二进制中的形式,这会导致较小的精度或舍入偏差。例如,
1.3
实际上可能表示为
1.299999999

因此,永远不要使用
=
来比较两个浮点数。相反,将这两个数字相减,并将它们与一个非常小的数字进行比较

对于您的情况,请尝试使用:

 if abs(sumAreas - tArea1) < 0.00001 then
此外,还建议,但不建议:

if Round(sumAreas) = Round(tArea1) then

参考资料:

您想做什么,遇到了什么问题?懒惰学生的作业?你想做什么,遇到什么问题?懒惰学生的家庭作业?你的IF有多余的括号。:-)是的。如果x>4,则是完美的fine@MarcovandeVoort好的,谢谢。我已经修改过了。这只是C的一个习惯:-)你的IF有多余的括号:-是的。如果x>4,则是完美的fine@MarcovandeVoort好的,谢谢。我已经修改过了。这只是C的一个习惯:-)