[SciPy-user] Vector mean calculation?

Lance Boyle lanceboyle at cwazy.co.uk
Sat Apr 23 01:06:13 CDT 2005

```On Apr 22, 2005, at 3:04 AM, Fernando.Perez at colorado.edu wrote:

> Lance Boyle wrote:
>> On Apr 21, 2005, at 5:41 PM, Fernando.Perez at colorado.edu wrote:
>>
>>
>>> Quoting Thomas Davidoff <davidoff at haas.berkeley.edu>:
>>>
>>>
>>>> I have a vector with min equal to 412.  Why am I getting a negative
>>>> mean?
>>>
>>> Without more details, it's quite difficult to be sure.  Most likely,
>>> because
>>> it's very long and of integer type (ints are 32 bit objects, so you
>>> are
>>> effectively doing mod 2^31 arithmetic with negative wraparound).
>>>
>>
>> Why does Python allow modular arithmetic when the user doesn't request
>> modular arithmetic?
>
> Well, it's just what happens in C always: regular ints are 32 bit
> objects, hence
> with a range of [-2**31,2**31-1].  If you start adding and go beyond
> the right
> endpoint of the interval, it will wrap around the left.  Pure python
> shields
> you from this with silent promotion to  python longs, which are
> arbitrary-length integers.  But in Numeric/numarray, you are using raw
> C
> objects (for speed reasons), so you can't escape some of their
> limitations
> (which are in many cases precisely what gives you speed).   Life is
> full of
> compromises...
>
> Best,
>
> f

Well, I apologize for a bit of baiting. I get upset when a language
lets me do something which I didn't intend, and when it is preventable
in the design of the language. Python is 30 years newer than C but yet
we have to put up with some of the same deficiencies of C (OK, so it
was in a library). If the original poster was really suffering
wrap-around (getting a mean value of a vector that is less than the
minimum value), then he/she could be the poster boy/girl for poor
language design. We might inquire about how much time he saved by using
unchecked modular arithmetic versus how much time he wasted debugging
his program, posting to this list, and making repairs. We might also
ask him what might have been the consequences of getting such an
incorrect result that he did not catch.

Flame away. 8^)

Lance

```