Inefficiency of `mod'
Maurice.Lombardi at ujf-grenoble.fr
Fri Oct 22 19:51:40 CEST 2010
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);
> i, j, m : integer;
> 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);
> [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.
Laboratoire de Spectrometrie Physique,
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 63 54 95
mailto:Maurice.Lombardi at ujf-grenoble.fr
More information about the Gpc