Recent

Author Topic: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!  (Read 931 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

  • Full Member
  • ***
  • Posts: 104
    • 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1075
  • Former Delphi 1-7, 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 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

Thaddy

  • Hero Member
  • *****
  • Posts: 10605
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 »

JdeHaan

  • Jr. Member
  • **
  • Posts: 60
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: 10605
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.

zamronypj

  • Full Member
  • ***
  • Posts: 104
    • 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

 

TinyPortal © 2005-2018