Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.9k stars 540 forks source link

"%" operator not always behaving as documented #18889

Open sisyphus opened 3 years ago

sisyphus commented 3 years ago

Description

When IV-precision is less than NV-precision, there can be values for which the "%" operator behaves in a way that does not conform with my understanding of the perlop documentation.

Steps to Reproduce

On a perl-5.34.0, whose ivsize is 8 and whose nvtype is __float128 I get:

C:\>perl -wle "$m = (2**113) - 1; $n = 2.0; print $m % $n;
0

The POSIX module disagrees:

C:\>perl -MPOSIX -wle "$m = (2**113) - 1; $n = 2.0; print fmod($m, $n);"
1

Expected behavior

I believe that the relevant pieces of perlop documentation regarding the behaviour of the % operator for these given values of $m and $n are:

If $n is positive, then "$m % $n" is $m minus the largest multiple of $n less than or equal to $m.
 ....
If the operands $m and $n are floating point values and the absolute value of $n (that is "abs($n)")
is less than "(UV_MAX + 1)", only the integer portion of $m and $n will be used in the operation 
(Note: here "UV_MAX" means the maximum of the unsigned integer type)

To me that documentation implies that the correct result for the given example is given by:

C:\>perl -wle "$r = 10384593717069655257060992658440191.0 - (5192296858534827628530496329220095.0 * 2.0); print $r;"
1

Similar behaviour exists when ivsize is 4, and nvtype is __float128:

C:\>perl -wle "$m = (2**113) - 1; $n = 2.0; print $m % $n;
0

C:\>perl -MPOSIX -wle "$m = (2**113) - 1; $n = 2.0; print fmod($m, $n);"
1

And also when ivsize is 4 and nvtype is long double:

C:\>perl -wle "$m = (2**64) - 1; $n = 2.0; print $m % $n;
0
C:\>perl -MPOSIX -wle "$m = (2**64) - 1; $n = 2.0; print fmod($m, $n);"
1

Oddly, however, all works as documented when ivsize is 4 and nvtype is double:

C:\>perl -wle "$m = (2**53) - 1; $n = 2.0; print $m % $n;
1

C:\>perl -MPOSIX -wle "$m = (2**53) - 1; $n = 2.0; print fmod($m, $n);"
1

Perl configuration

Summary of my perl5 (revision 5 version 34 subversion 0) configuration:

  Platform:
    osname=MSWin32
    osvers=6.1.7601
    archname=MSWin32-x64-multi-thread-quadmath
    uname=''
    config_args='undef'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='gcc'
    ccflags =' -DWIN32 -DWIN64 -fdiagnostics-color=never -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -D__USE_MINGW_ANSI_STDIO -fwrapv -fno-strict-aliasing -mms-bitfields'
    optimize='-s -O2'
    cppflags='-DWIN32'
    ccversion=''
    gccversion='10.2.0'
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long long'
    ivsize=8
    nvtype='__float128'
    nvsize=16
    Off_t='long long'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='g++'
    ldflags ='-s -L"C:\perl-5.34.0\lib\MSWin32-x64-multi-thread-quadmath\CORE" -L"C:\_64\winlibs-gcc-1020\mingw64\lib" -L"C:\_64\winlibs-gcc-1020\mingw64\x86_64-w64-mingw32\lib" -L"C:\_64\winlibs-gcc-1020\mingw64\lib\gcc\x86_64-w64-mingw32\10.2.0"'
    libpth=C:\_64\winlibs-gcc-1020\mingw64\lib C:\_64\winlibs-gcc-1020\mingw64\x86_64-w64-mingw32\lib C:\_64\winlibs-gcc-1020\mingw64\lib\gcc\x86_64-w64-mingw32\10.2.0 C:\_64\msys_1020\1.0\local\lib
    libs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lquadmath
    perllibs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lquadmath
    libc=
    so=dll
    useshrplib=true
    libperl=libperl534.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-mdll -s -L"C:\perl-5.34.0\lib\MSWin32-x64-multi-thread-quadmath\CORE" -L"C:\_64\winlibs-gcc-1020\mingw64\lib" -L"C:\_64\winlibs-gcc-1020\mingw64\x86_64-w64-mingw32\lib" -L"C:\_64\winlibs-gcc-1020\mingw64\lib\gcc\x86_64-w64-mingw32\10.2.0"'

Characteristics of this binary (from libperl):
  Compile-time options:
    HAS_TIMES
    HAVE_INTERP_INTERN
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_IMPLICIT_CONTEXT
    PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_QUADMATH
  Built under MSWin32
  Compiled at May 21 2021 20:11:53
  @INC:
    C:/perl-5.34.0/site/lib/MSWin32-x64-multi-thread-quadmath
    C:/perl-5.34.0/site/lib
    C:/perl-5.34.0/lib/MSWin32-x64-multi-thread-quadmath
    C:/perl-5.34.0/lib
oodler577 commented 3 years ago

Minor comment - updating % in the title to read modulus might attract some more attention to this.