develooper Front page | perl.perl5.porters | Postings from March 2003

[perl #21770] modulus function not working for negative numbers

Thread Next
From:
perlbug-followup
Date:
March 31, 2003 18:39
Subject:
[perl #21770] modulus function not working for negative numbers
Message ID:
rt-21770-54401.11.4701280647837@bugs6.perl.org
# New Ticket Created by  bplatz@acm.org 
# Please include the string:  [perl #21770]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21770 >



This is a bug report for perl from bplatz@acm.org,
generated with the help of perlbug 1.26 running under perl 5.00503.

-----------------------------------------------------------------
[Please enter your report here]


Dear Perl Support,

Perl's modulus (%) operator produces incorrect values for negative numbers
under both Perl 5.005 and Perl 5.8, as seen in the example below:

aldrsh(meappdev/megpsrv): z.pl
a = 1, b = 2
aldrsh(meappdev/megpsrv): cat z.pl
#!/opt/perl58/bin/perl -w

my $a = -5 % 3;
my $b = 5 % 3;

print "a = $a, b = $b\n";

aldrsh(meappdev/megpsrv): z.pl
a = 1, b = 2

In the example above, 2 should be reported for the value of $a, not 1.
Looks like subtraction is being used to perform this calcluation, and
abs(modulus operand) is not performed before substraction begins.

[Please do not change anything below this line]
-----------------------------------------------------------------

---
Site configuration information for perl 5.00503:

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=solaris, osvers=2.8, archname=sun4-solaris
    uname='sunos localhost 5.8 sun4u sparc sunw,ultra-1 '
    hint=previous, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-xO3 -xdepend', gccversion=
    cppflags=''
    ccflags =''
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =''
    libpth=/lib /usr/lib /usr/ccs/lib
    libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
    libc=/lib/libc.so, so=so, useshrplib=true, libperl=libperl.so
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-R /usr/perl5/5.00503/sun4-solaris/CORE'
    cccdlflags='-KPIC', lddlflags='-G'

Locally applied patches:
    

---
@INC for perl 5.00503:
    /usr/perl5/5.00503/sun4-solaris
    /usr/perl5/5.00503
    /usr/perl5/site_perl/5.005/sun4-solaris
    /usr/perl5/site_perl/5.005
    .

---
Environment for perl 5.00503:
    HOME=/home/aldrsh
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/opt/SUNWspro6/SUNWspro/WS6/lib:/usr/lib:/sopt/app/oracle/product/7.3.4/lib:/usr/dt/lib:/usr/dt/lib
    LOGDIR (unset)
    PATH=/sopt/app/oracle/product/7.3.4/bin:/usr/openwin/bin:/usr/dt/bin:/usr/openwin/lib/X11:/usr/openwin/bin:/sopt/app/oracle/product/7.3.4/bin:/opt/oracle/local/bin:/usr/ccs/bin:/opt/SUNWspro6/SUNWspro/bin:/home/aldrsh/bin:/opt/addpath/bin:/usr/ccs/bin:/usr/bin:/usr/ucb:/usr/openv/netbackup/bin:/opt/VRTSvmsa/bin:/opt/sudo/bin:/opt/local/bin:/opt/f-secure-ssh/bin:/usr/openv/netbackup/bin:/usr/lib:/sopt/dazel/local/bin::/opt/dcs/bin:/opt/dcs/adm:/opt/local/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/usr/sadm/install/bin:/opt/ncr/terajdbc/bin:/opt/EMCpower/bin:/etc:/sbin:/usr/sbin:/usr/bin:/usr/sadm/install/bin:/opt/ncr/terajdbc/bin::/usr/dt/bin:/opt/smradmin/scheduler/dcssrv15eP:/opt/smradmin/bin:/home/unidats/UNI_DEVTOOLS/bin:/opt/oracle/local/bin:/opt11/matrix/local/bin:/usr/local/bin:/bin:/usr/ccs/bin
    PERL_BADLANG (unset)
    SHELL=/bin/ksh


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About