Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

% does not work as documented for non-integer operand #1862

Closed
p5pRT opened this issue Apr 19, 2000 · 11 comments
Closed

% does not work as documented for non-integer operand #1862

p5pRT opened this issue Apr 19, 2000 · 11 comments

Comments

@p5pRT
Copy link

p5pRT commented Apr 19, 2000

Migrated from rt.perl.org#3127 (status was 'resolved')

Searchable as RT3127$

@p5pRT
Copy link
Author

p5pRT commented Apr 19, 2000

From @jimav

This is a bug report for perl from avera@​hal.com,
generated with the help of perlbug 1.26 running under perl 5.00502.


% perl -we 'printf "%f\n", (1.5 % 8)'
1.00000

The documentation says
  "If $b is positivie, then $a % $b is $a minus the largest multiple
  of $b that is not greater than $a."

The largest multiple of 8 which is not greater than 1.5 is 0,
so the answer should be 1.5 - 0 = 1.5.

The docs do not say anything about truncating the result to an integer.

Is this a perl bug or a doc bug?

-Jim Avera (jima@​hal.com)



Site configuration information for perl 5.00502​:

Configured by avera at Mon Jan 18 16​:36​:45 PST 1999.

Summary of my perl5 (5.0 patchlevel 5 subversion 2) configuration​:
  Platform​:
  osname=solaris, osvers=2.5.1, archname=sun4-solaris
  uname='sunos membrane 5.5.1 generic_103640-21 sun4u sparc sunw,ultra-5_10 '
  hint=recommended, useposix=true, d_sigaction=define
  usethreads=undef useperlio=undef d_sfio=undef
  Compiler​:
  cc='/sim/caetools_ssd1/VCS/cctools/sun4-solaris/cc', optimize='-g -O', gccversion=2.7.2.1
  cppflags='-DDEBUGGING'
  ccflags ='-DDEBUGGING'
  stdchar='unsigned 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=y, prototype=define
  Linker and Libraries​:
  ld='/sim/caetools_ssd1/VCS/cctools/sun4-solaris/cc', ldflags =' -L/usr/local/lib'
  libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib
  libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
  libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
  cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib'

Locally applied patches​:
 


@​INC for perl 5.00502​:
  /sim/vcsperl/perl/5.005_02/lib/5.00502/sun4-solaris
  /sim/vcsperl/perl/5.005_02/lib/5.00502
  /sim/vcsperl/perl/5.005_02/lib/site_perl/5.005/sun4-solaris
  /sim/vcsperl/perl/5.005_02/lib/site_perl/5.005
  .


Environment for perl 5.00502​:
  HOME=/u/avera
  LANG (unset)
  LC_COLLATE=en_US
  LC_CTYPE=en_US
  LC_MESSAGES=C
  LC_MONETARY=en_US
  LC_NUMERIC=en_US
  LC_TIME=en_US
  LD_LIBRARY_PATH=/usr/is/lib​:/usr/lib​:/usr/openwin/lib​:/usr/ucblib
  LOGDIR (unset)
  PATH=/sim/vcsperl/perl/new/bin​:/u/avera/bin​:/u/avera/bin/misc​:/usr/is/bin​:/usr/ccs/bin​:/usr/is/gnu/bin​:/usr/bin​:/usr/sbin​:/sbin​:/opt/SUNWspro-5.1/bin​:/usr/ucb​:.​:/sim/avera/cvshome/bin​:/u/avera/bin/hal​:/sim/tools/current/bin​:/usr/openwin/bin/.​:/usr/openwin/bin​:/sim/lb/current/bin​:/tools/all/qa/current/bin​:/tools/all/bin​:/u/avera/bin/ssdhosts​:/u/avera/bin/ws​:/distrib/SunOS__5.6__sun4m/frame-5.5.6/bin
  PERL_BADLANG (unset)
  SHELL=/usr/is/gnu/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Apr 19, 2000

From @tamias

On Wed, Apr 19, 2000 at 04​:20​:10PM -0700, Jim Avera wrote​:

% perl -we 'printf "%f\n", (1.5 % 8)'
1.00000

The documentation says
"If $b is positivie, then $a % $b is $a minus the largest multiple
of $b that is not greater than $a."

The largest multiple of 8 which is not greater than 1.5 is 0,
so the answer should be 1.5 - 0 = 1.5.

The docs do not say anything about truncating the result to an integer.

Is this a perl bug or a doc bug?

Doc bug. Modulus is, by definition, an integer operation, and the operands
are treated as integers.

Ronald

@p5pRT
Copy link
Author

p5pRT commented Apr 19, 2000

From [Unknown Contact. See original ticket]

Ronald J Kimball writes​:

Doc bug. Modulus is, by definition, an integer operation, and the operands
are treated as integers.

For some value of "integer" only. And not for the value used in other
private parts of Perlian logic.

The reason for the current semantic is backward compatibility only.
Do not try to find any "theoretical" background...

Ilya

@p5pRT
Copy link
Author

p5pRT commented Apr 19, 2000

From [Unknown Contact. See original ticket]

% perl -we 'printf "%f\n", (1.5 % 8)'
1.00000

The documentation says
"If $b is positivie, then $a % $b is $a minus the largest multiple
of $b that is not greater than $a."

The largest multiple of 8 which is not greater than 1.5 is 0,
so the answer should be 1.5 - 0 = 1.5.

How do you treat 0 as a multiple?
You will never get something other than 0 when you product 0 with something
and do that in real numbers.

1 will suit more likely.

May be 1.499999999 (or even 1.5)?
Because of fact that 8 = 1.5 * 5.3333(3)

The docs do not say anything about truncating the result to
an integer.

It goes into integers when it says about multiples. Note that you also
started finding *integer* that will suit as multiple.

Good luck,
Vadim.

@p5pRT
Copy link
Author

p5pRT commented Apr 20, 2000

From @jimav

Ronald J Kimball <rjk@​linguist.dartmouth.edu> wrote​:
] On Wed, Apr 19, 2000 at 04​:20​:10PM -0700, Jim Avera wrote​:
]
] > % perl -we 'printf "%f\n", (1.5 % 8)'
] > 1.00000
] ...
] > Is this a perl bug or a doc bug?
]
] Doc bug. Modulus is, by definition, an integer operation, and the operands
] are treated as integers.

What is the meaning (or intent) of the documentations' remark

  "Note than when
  use integer is in scope, "%" give you direct access to the
  modulus operator as implemented by your C compiler"

That is, if 'use integer' has some effect, what is the effect of
NOT doing use integer?

-Jim.

@p5pRT
Copy link
Author

p5pRT commented Apr 20, 2000

From @tamias

On Thu, Apr 20, 2000 at 09​:28​:45AM -0700, Jim Avera wrote​:

What is the meaning (or intent) of the documentations' remark

 "Note than when
 use integer is in scope\, "%" give you direct access to the
 modulus operator as implemented by your C compiler"

That is, if 'use integer' has some effect, what is the effect of
NOT doing use integer?

With 'use integer', % uses the C compiler's modulus operator directly.
Without 'use integer', % uses Perl's implementation of the modulus
operator. I think the main difference is in the result with negative
operands.

Ronald

@p5pRT
Copy link
Author

p5pRT commented Apr 20, 2000

From [Unknown Contact. See original ticket]

Jim Avera <avera@​hal.com> wrote

What is the meaning (or intent) of the documentations' remark

 "Note than when
 use integer is in scope\, "%" give you direct access to the
 modulus operator as implemented by your C compiler"

Err... what it says? It's acting as a qualification on the definition
of % which appears in the previous sentences.

That is, if 'use integer' has some effect, what is the effect of
NOT doing use integer?

It does what it's said elsewhere in the documentation to do​:

  Binary "%" computes the modulus of two numbers. Given
  integer operands $a and $b​: If $b is positive, then $a % $b
  is $a minus the largest multiple of $b that is not greater
  than $a. If $b is negative, then $a % $b is $a minus the
  smallest multiple of $b that is not less than $a (i.e. the
  result will be less than or equal to zero).

Looks quite complete to me.

It isn't very sensible quoting single sentences from the documentation
without context.

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Apr 20, 2000

From [Unknown Contact. See original ticket]

Jim Avera <avera@​hal.com> wrote

The documentation says
"If $b is positivie, then $a % $b is $a minus the largest multiple
of $b that is not greater than $a."

The docs do not say anything about truncating the result to an integer.

Eh? You're quoting out of context (and making the mistake of retyping
rather than inserting the actual text, judging by the typo). Let's do
it properly​:

  Binary "%" computes the modulus of two numbers. Given
  integer operands $a and $b​: If $b is positive, then $a % $b
  ^^^^^^^
  is $a minus the largest multiple of $b that is not greater
  than $a.

Looks clear enough to me.

Of course, the operator is left totally undefined if the operands are
not integers. WWhich leaves future Perls the option of implementing
% "correctly" in this case.

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Apr 24, 2000

From @ysth

In article <20000419235456.A775265@​linguist.dartmouth.edu>,
Ronald J Kimball <rjk@​linguist.dartmouth.edu> wrote​:

Doc bug. Modulus is, by definition, an integer operation, and the operands
are treated as integers.

Not in my universe. A self-consistent definition of real number
modulus is both possible and useful. See susv2​:

The Single UNIX (R) Specification, Version 2
Copyright (C) 1997 The Open Group

fmod - floating-point remainder value function

SYNOPSIS

#include <math.h>

double fmod(double x, double y);

DESCRIPTION The fmod() function returns the floating-point remainder
of the division of x by y.

An application wishing to check for error situations should set errno
to 0 before calling fmod(). If errno is non-zero on return, or the
return value is NaN, an error has occurred.

RETURN VALUE

The fmod() function returns the value x - i * y for some integer i
such that, if y is non-zero, the result has the same sign as x and
magnitude less than the magnitude of y.

If x or y is NaN, NaN is returned and errno may be set to [EDOM].

If y is 0, NaN is returned and errno is set to [EDOM], or 0 is
returned and errno may be set to [EDOM].

If x is +/-Inf, either 0 is returned and errno is set to [EDOM], or
NaN is returned and errno may be set to [EDOM].

If y is non-zero, fmod(+/-Inf,y) returns the value of x. If x is not
+/-Inf, fmod(x,+/-Inf) returns the value of x.

If the result underflows, 0 is returned and errno may be set to
[ERANGE].

ERRORS

The fmod() function may fail if​:

[EDOM] One or both of the arguments is NaN, or y is 0, or x is +/-Inf.

[ERANGE] The result underflows.

No other errors will occur.

EXAMPLES

None.

APPLICATION USAGE

Portable applications should not call fmod() with y equal to 0,
because the result is implementation-dependent. The application
should verify y is non-zero before calling fmod().

FUTURE DIRECTIONS

None.

SEE ALSO

isnan(), <math.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX (R) is a registered Trademark of The Open Group.
Copyright (C) 1997 The Open Group

@p5pRT
Copy link
Author

p5pRT commented Aug 2, 2008

From p5p@spam.wizbit.be

Clarifications were added in the docs for operands that are floating
points.

@p5pRT
Copy link
Author

p5pRT commented Aug 2, 2008

p5p@spam.wizbit.be - Status changed from 'stalled' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant