[SciPy-user] Vector mean calculation?

Thomas Davidoff davidoff at haas.berkeley.edu
Sat Apr 23 12:04:52 EDT 2005



Bob Ippolito wrote:

> On Apr 23, 2005, at 2:06 AM, Lance Boyle wrote:
>
>> 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...
>>
>>
>> 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.
>
>
> 30 years in what universe?  C has been around for about 34 years 
> (1971), and Python wasn't born yesterday (1990).
>
> Anyway, you could ask him if he would prefer a default implementation 
> that ran 10x slower (probably worse) because it performed checked 
> arithmetic.  You get one or the other, and both is simply more work.  
> If this function was 100% Python and didn't bother with Numeric/etc. 
> it would've done the right thing out of the box, but it might still be 
> calculating :)
>
> -bob
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

 From the original poster: the mean miscalculation was the least of my 
programming problems in this exercise (so far) - not a bad learning 
experience.




More information about the SciPy-User mailing list