Robert Bradshaw wrote:
> On Oct 8, 2009, at 10:54 AM, Dag Sverre Seljebotn wrote:
> 
>> Robert Bradshaw wrote:
>>> On Oct 8, 2009, at 6:04 AM, Dag Sverre Seljebotn wrote:
>>>
>>>> I experience this:
>>>>
>>>> <Py_ssize_t>np.sqrt(some_float64)
>>>>  => TypeError: 'numpy.float64' object cannot be interpreted as an
>>>> index
>>>>
>>>> However
>>>>  a) <int>np.sqrt(...) works fine, as does <long> and <long long>.
>>>>  b) In Python, int(np.sqrt(...)) and long(np.sqrt(...)) both works
>>>> fine
>>>>
>>>> Is Py_ssize_t "special" when it comes to converting from Python
>>>> objects?
>>>> If so I find it mildly confusing and something which only increases
>>>> learning curve...
>>> Py_ssize_t is meant for indexing, and fails for the same reason that
>>>
>>>>>> L = range(10)
>>>>>> L[2]
>>> 2
>>>>>> L[2.5]
>>> Traceback (most recent call last):
>>>    File "<stdin>", line 1, in <module>
>>> TypeError: list indices must be integers, not float
>>>
>>> fails. This is a feature, not a bug.
>> OK, if this is a feature, help me out with this then:
>>
>> I need to iterate from 0 to "lmax". When I allocate an array, it is
>> always allocated of size (lmax + 1)**2, and it is convenient to be  
>> able
>> to deduce lmax from the array shape.
>>
>> What type should I use for lmax to conveniently convert from a float
>> (because sqrt is float, but I know I can safely truncate) and  
>> guarantee
>> that I support the range of NumPy arrays (which essentially use
>> Py_ssize_t to store shape info)? The type must be signed, otherwise I
>> fall into the range(-lmax, lmax) trap all the time.
> 
> Is there not a ssize_t?

Not builtin to Cython, I have to extern ctypedef it.

I know that *I* can get around it -- I'm mostly worried about what I 
should recommend to NumPy users (I have been recommending Py_ssize_t, 
but now I see that might not have been a good idea).

But there's enough going on at the moment, so I'll stop short of trying 
to find better solutions for now. Thanks for clearing it up.

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to