This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients mark.dickinson
Date 2017-04-01.14:32:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1491057158.44.0.328286654069.issue29962@psf.upfronthosting.co.za>
In-reply-to
Content
IEEE 754, the C99 standard, the Decimal IBM standard and Java all support/specify a 'remainder-near' operation. Apart from being standard, this has a number of useful applications:

1. Argument reduction in numerical algorithms: it's common to want to reduce to a range [-modulus/2, modulus/2] rather than [0, modulus).
2. Particular case of the above: reduction of angles to lie in the range [-pi, pi]
3. Rounding a float x to the nearest multiple of y. This is a much-asked StackOverflow question, and the standard answer of y * round(x / y) risks introducing floating-point error and so can give incorrect results in corner cases. With a remainder operation, it's trivial to do this correctly: x - remainder(x, y) gives the closest representable float to the closest integer multiple of y to x.

remainder(x, y) has some nice properties: it's *always* exactly representable (unlike x % y), it satisfies the symmetry remainder(-x, y) == -remainder(x, y), and it's periodic with period 2*y.

I have a patch, and will make a PR shortly.
History
Date User Action Args
2017-04-01 14:32:38mark.dickinsonsetrecipients: + mark.dickinson
2017-04-01 14:32:38mark.dickinsonsetmessageid: <1491057158.44.0.328286654069.issue29962@psf.upfronthosting.co.za>
2017-04-01 14:32:38mark.dickinsonlinkissue29962 messages
2017-04-01 14:32:37mark.dickinsoncreate