Robert Bradshaw wrote:
> On Oct 8, 2009, at 11:13 AM, Dag Sverre Seljebotn wrote:
> 
>> 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.
> 
> Well, if we have size_t, we should probably support ssize_t as well.
> 
> http://trac.cython.org/cython_trac/ticket/399

I think I figured out a nice way to fix this issue in general 
though...optimize

some_int = int(some_float)

Nice and Pythonic and one doesn't have to bother with casts, types etc.

http://trac.cython.org/cython_trac/ticket/400

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

Reply via email to