Recent

Author Topic: Help fixing: Error: Incompatible types: got "Boolean" expected "Int64"!  (Read 2407 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: 133
    • 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
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
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 »
Specialize a type, not a var.

JdeHaan

  • Full Member
  • ***
  • Posts: 115
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: 14205
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • 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
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

 

TinyPortal © 2005-2018