MLUG: Re: [UUG/MLUG] perl and y2k
Re: [UUG/MLUG] perl and y2k
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Actually, I *think* you guys have it backwards.  Perl is broken.

or perhaps Matlab is broken too, or I'm reading things wrong.  But I
usually trust C on this stuff.

http://www.ind2.polymtl.ca/matlab/techdoc/ref/mod.html (matlab
documentation for mod)

it claims modulus is the *signed* remainder after division.

-T

On Wed, 5 Jan 2000, Jonathan King wrote:

> 
> On Wed, 5 Jan 2000, Stephen Montgomery-Smith wrote:
> 
> > Jonathan King wrote:
> >
> 
> [snip]
> 
> > > Along these lines, and while we all run off to fix our code, some of you
> > > might be interested in the fact that perl does something potentially
> > > unexpected with the % operator when the first argument is negative, so
> > > that:
> > > 
> > >     [EMAIL:PROTECTED]$ perl -e '$a=-98; printf("%02d\n",$a%100);'
> > >     02
> > > 
> > 
> > Hmmm, in my opinion (as a mathematician) that is the correct answer.
> > I would have expected -98 (in my opinion the wrong answer, but
> > what I think C does).
> 
> Curiousity caused the cat:
> 
>     [EMAIL:PROTECTED king]$ cat > triv.c
>     int main () {
>     printf("%d\n", -98%100);
>     }
>     [EMAIL:PROTECTED king]$ cc -o triv triv.c
>     [EMAIL:PROTECTED king]$ triv
>     bash: triv: command not found
> 
> Hmm...I wonder why this happened. :-)
> 
>     [EMAIL:PROTECTED king]$ ./triv
>     -98
> 
> And, indeed, it is the fact that both C and awk diverges from mathematical
> convention that causes people to blame perl for a "wrong" answer in this
> case.
> 
> jking
>