Inefficiency of `mod'

Adriaan van Os gpc at microbizz.nl
Mon Oct 25 09:39:58 CEST 2010


Frank Heckenbach wrote:
> Lennart Thelander wrote:
> 
>> I disagree somewhat.
>> In this case the '-' is a unary operator, not an additive operator. A unary
>> operator should evaluate before any other operator, at least in this case.
>> The -10 should evaluate immediately as the simple value it is. Then the mod
>> operator is evaluated.
>> So you should not have to put any parenthesis around -10 to obtain the
>> correct result. 
> 
> Well, that's your personal opinion which is shared by C and related
> languages, including Borland Pascal.
> 
> But Adriaan explicitly asked about standard Pascal which follows the
> mathematical convention (see ISO 7185, 6.7.1) where there's no
> special priority of unary operators, e.g. in mathematics -2^2 equals
> -4, not 4.

You are right. But I wonder how many Pascal programmers are wrong here.... It is very tricky. What 
about a compiler warning ?

Another example:

program unary;
begin
   writeln( -2 shr 1)
end.

[P18:~] adriaan% gp --borland-pascal unary.pas
[P18:~] adriaan% ./unary
-1

[P18:~] adriaan% fpc -Mtp unary.pas
Free Pascal Compiler version 2.5.1 [2010/10/19] for i386
Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Darwin for i386
Compiling unary.pas
Assembling unary
Linking unary
6 lines compiled, 0.1 sec
[P18:~] adriaan% ./unary
9223372036854775807

Regards,

Adriaan van Os





More information about the Gpc mailing list