[SciPy-dev] modulo operation and new scipy core

Arnd Baecker arnd.baecker at web.de
Wed Oct 12 02:58:14 CDT 2005


Hi,

one thing which I find irritating is the behaviour of
the modulo operation for arrays:

Compare:

In [1]: from scipy import *
In [2]: -0.4 % 1.0
Out[2]: 0.59999999999999998
In [3]: x=arange(-0.6,1.0,0.1)
In [4]: x%1.0
Out[4]:
array([ -6.00000000e-01,  -5.00000000e-01,  -4.00000000e-01,
        -3.00000000e-01,  -2.00000000e-01,  -1.00000000e-01,
         1.11022302e-16,   1.00000000e-01,   2.00000000e-01,
         3.00000000e-01,   4.00000000e-01,   5.00000000e-01,
         6.00000000e-01,   7.00000000e-01,   8.00000000e-01,
         9.00000000e-01])

Even worse (IMHO): take a scalar (I know it is still an array,
but it does not look like one ;-) from the array

In [5]: x[2]
Out[5]: -0.39999999999999997

In [6]: x[2] % 1.0
Out[6]: -0.39999999999999997

It seems that for arrays % behaves like `fmod` and not like `mod`.

I find this confusing as it is in contrast to the
python 2.4 documentation:

"5.6. Binary arithmetic operations"

   """The % (modulo) operator yields the remainder from the division
      of the first argument by the second. [...]
      The arguments may be floating point numbers, e.g.,
      3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.)
      The modulo operator always yields a result with the same sign as
      its second operand (or zero); the absolute value of the result
      is strictly smaller than the absolute value of the second
      operand."""

Would it be possible for the new scipy core that % behaves
the same (standard python) way for scalars and for arrays?

Best,

Arnd




More information about the Scipy-dev mailing list