线性方程,不兼容的类型 BOOLEAN/LONGINT

Linear equation, incompatible types BOOLEAN/LONGINT

我在 Pascal 中进行了有关线性方程的练习,并且我已经创建了用于比较输入数字的简单代码,但是当我尝试 运行 它时。我有不兼容类型的问题,got BOOLEAN and expected LONGINT.

program LinearEquation;

var
  a, b: real;

begin
  readln(a, b);

  if (b = 0 and a = 0) then
    writeln('INFINITY')
  else if (b = 0 and a <> 0) then
    writeln(1)
  else if (a = 0 and b <> 0) then
    writeln(0)
  else if(b mod a = 0) then
    writeln(1);

  readln;

end.

13 / 9 rownan~1.pas
 Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
15 / 14 rownan~1.pas
 Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
17 / 14 rownan~1.pas
 Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
17 / 14 rownan~1.pas
 Error: Incompatible types: got "BOOLEAN" expected "LONGINT"

至少在modern Delphi中,and的优先级高于=,所以

a = 0 and b = 0

被解释为

(a = (0 and b)) = 0.

但是 and 运算符不能接受整数和浮点值作为操作数(尽管两个整数也可以)。因此错误。

如果ab是整数,0 and b就是0b的按位合取,即0.因此,我们本来可以

(a = 0) = 0.

这读作 true = 0(如果 a 等于 0)或 false = 0(如果 a 不同于 0 ).但是布尔值不能与整数进行比较,因此编译器会对此进行抱怨。

不过,这只是一个学术练习。显然,您的意图是

(a = 0) and (b = 0).

只需添加括号:

if (b = 0) and (a = 0) then
  writeln('INFINITY')