Why is 7 / 2 not an Integer? The compiler is angry with me

Go down

Why is 7 / 2 not an Integer? The compiler is angry with me

· #1

Coming from JavaScript, I wrote this:

pascal
var
  Half: Integer;
begin
  Half := 7 / 2;
end.

and got:

text
Error: Incompatible types: got "Double" expected "LongInt"

In my old life this would just work. Badly, but it would work.

Re: Why is 7 / 2 not an Integer? The compiler is angry with me

· #2

That sentence is the whole difference between the two languages.

In Pascal / always gives a floating-point result, even for two integers. For whole-number division there are two operators of their own:

You write You get Example
7 / 2 a Double 3.5
7 div 2 the whole part 3
7 mod 2 the remainder 1

So:

pascal
Half := 7 div 2;        // 3
Half := Round(7 / 2);   // 4, banker's rounding applies to .5 cases
Half := Trunc(7 / 2);   // 3

The compiler is not angry. It is asking which one you meant, which is more than JavaScript ever did for you.

Re: Why is 7 / 2 not an Integer? The compiler is angry with me

· #3

which is more than JavaScript ever did for you

I feel seen. Thank you.

Re: Why is 7 / 2 not an Integer? The compiler is angry with me

· #4

WAIT SO / AND div ARE DIFFERENT

i have been using / for everything since 2011. sorry about the caps

SORRY ABOUT THE CAPS

Re: Why is 7 / 2 not an Integer? The compiler is angry with me

· #5

That explains the invoice program.

Forum rules 1 to 10 apply. Yes, to you.

Go up

Sign in to reply