Message290985
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. |
|
Date |
User |
Action |
Args |
2017-04-01 14:32:38 | mark.dickinson | set | recipients:
+ mark.dickinson |
2017-04-01 14:32:38 | mark.dickinson | set | messageid: <1491057158.44.0.328286654069.issue29962@psf.upfronthosting.co.za> |
2017-04-01 14:32:38 | mark.dickinson | link | issue29962 messages |
2017-04-01 14:32:37 | mark.dickinson | create | |
|