Inefficiency of `mod'

Lennart Thelander Lennart.Thelander at atex.com
Sat Oct 23 14:04:11 CEST 2010


Hello.

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. 

Regards
Lennart Thelander

On 10-10-22 19.51, "Maurice Lombardi" <Maurice.Lombardi at ujf-grenoble.fr>
wrote:

> Le 22/10/2010 13:57, Adriaan van Os a écrit :
>> Frank Heckenbach wrote:
>>> CBFalconer wrote:
>>> 
>>>> Adriaan van Os wrote:
>>>>> I might be interesting to note that not all Pascal compilers
>>>>> follow the ISO 7185 Pascal standard for the mod operator. The
>>>>> standard states:
>>>> [...]
>>>> 
>>>> This is the result of Borland ignoring the standard. There is no
>>>> excuse, because the standard was available long before the first
>>>> Turbo Pascal. The drafts were published in Pascal News in the mid
>>>> '70s. It is one of the reasons many will not consider
>>>> Delphi/Borland/Turbo to be Pascal.
>>> 
>>> That's another reason why GPC's `mod' implementation is so hairy
>>> (because it emulates Borland's in `--borland-pascal') ...
>> 
>> It looks like there is still a problem in the compiler for constant
>> expressions using the mod operator.
>> 
>> program modulo( Output);
>> var
>> i, j, m : integer;
>> begin
>> i := -10;
>> j := 100;
>> m := i mod j;
>> Writeln( ' m = ', i: 3, ' mod ', j: 3, ' = ', m: 3);
>> Writeln( '-10 mod 100 = ', -10 mod 100 :3);
>> end.
>> 
>> [P18:~] adriaan% gp --standard-pascal modulo.pas
>> [P18:~] adriaan% ./modulo
>> m = -10 mod 100 = 90
>> -10 mod 100 = -10
> 
> No, this is a problem of precedence of  operators.
> mod, being a "kind of" division operator, has precedence on -,
> an additive operator, in standard pascal.
> simply replace the penultimate line by
> 
> Writeln( '(-10) mod 100 = ', (-10) mod 100 :3);
> 
> and the  result is correct.
> 
> Maurice






More information about the Gpc mailing list