Recent

Author Topic: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!  (Read 611 times)

Sheeper

  • Newbie
  • Posts: 1
This error appeared while I was doing a simple program, really need help to fix it. Thank you!!

Program New_1;
Uses Crt;
Var x, F:Real;
Begin
Clrscr;
Write('Type x='); Readln(x);
If 0<x<=1 then
 F:=4*Sqr(x)-5*x+1
Else{x<=0 or x>1}
 F:= Abs(x-2)/3*(sqr(x)+1);
Writeln('F(x)=', F); Readln
End. 

zamronypj

  • Jr. Member
  • **
  • Posts: 59
    • Fano Framework, Free Pascal web application framework
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #1 on: January 23, 2020, 04:15:13 am »
I suggest you to read https://www.freepascal.org/docs-html/current/ref/ref.html first to learn Pascal syntax.

0<x<=1 is not pascal syntax. But it consists of two boolean comparison

0<x and x<=1

So if comparison should be written as

if 0<x and x<=1 then
« Last Edit: January 23, 2020, 04:19:39 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Personal Projects https://v3.juhara.com
Github https://github.com/zamronypj

trev

  • Sr. Member
  • ****
  • Posts: 407
  • Former Delphi 7 and Delphi 10.2 User
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #2 on: January 23, 2020, 08:29:38 am »
@Sheeper: see the Wiki at https://wiki.lazarus.freepascal.org/Forum for how to properly format code in the Forum.
o Lazarus v2.1.0 r62452, FPC v3.3.1 r42644, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

Thaddy

  • Hero Member
  • *****
  • Posts: 9643
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #3 on: January 23, 2020, 08:45:39 am »
I suggest you to read https://www.freepascal.org/docs-html/current/ref/ref.html first to learn Pascal syntax.

0<x<=1 is not pascal syntax. But it consists of two boolean comparison

0<x and x<=1
Correct. Alternatively InRange() from the math unit:
Code: Pascal  [Select]
  1. if InRange(x,0,1) then...
Also note real is unlike C a distinct 48 bit Pascal type that is deprecated. For modern Pascal code use single or double instead. real is now an alias for double.
(But FreePascal has real48 support for compatibility with older code, mainly for I/O)

An example with formatted output would look like this:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2. Program New_1;
  3. Uses Crt, math;
  4. Var
  5.   X, F:Real;
  6. Begin
  7.   Clrscr;
  8.   Write('Type x='); Readln(x);
  9.   If InRange(x,0,1) then
  10.     F:=4*Sqr(x)-5*x+1
  11.   Else
  12.     F:= Abs(x-2)/3*(sqr(x)+1);
  13.   Writeln('F(x)=', F:1:4);
  14.   Readln;
  15. End.

« Last Edit: January 23, 2020, 09:02:49 am by Thaddy »
I am more like donkey than shrek

JdeHaan

  • New Member
  • *
  • Posts: 38
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #4 on: January 23, 2020, 08:57:13 am »
if 0<x and x<=1 then

Note that 'and' has higher precedence than '<' and '<=', so you should use parenthesis '(' and ')'.

Thaddy

  • Hero Member
  • *****
  • Posts: 9643
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #5 on: January 23, 2020, 09:06:05 am »
if 0<x and x<=1 then

Note that 'and' has higher precedence than '<' and '<=', so you should use parenthesis '(' and ')'.
That's correct but confusing. Why not write
Code: Pascal  [Select]
  1. if (0<x) and (x<=1) then
because that is what you mean.
I am more like donkey than shrek

zamronypj

  • Jr. Member
  • **
  • Posts: 59
    • Fano Framework, Free Pascal web application framework
Re: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!
« Reply #6 on: January 24, 2020, 02:29:02 am »
if 0<x and x<=1 then

Note that 'and' has higher precedence than '<' and '<=', so you should use parenthesis '(' and ')'.

@JdeHaan You are correct.
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Personal Projects https://v3.juhara.com
Github https://github.com/zamronypj