Stefan Behnel wrote: > Dag Sverre Seljebotn, 03.12.2009 12:45: >> Stefan Behnel wrote: >>> Dag Sverre Seljebotn, 03.12.2009 10:52: >>> >>>>> - Python's floating point type is double, so it should be perfectly >>>>> safe to do >>>>> >>>>> cdef double f(): ... >>>>> x = f() # infer x as double >>>>> >>>>> By the same argument one might actually want to do >>>>> >>>>> cdef float f(): ... >>>>> x = f() # infer x as *double* -- because that's what Python would use >>>>> >>> That's actually an orthogonal feature: aliasing Python's float type to C's >>> double type in general, including support for methods etc. Since 'double' >>> is immutable and has exactly the same value range as Python's float type, >>> this is something that can always be done safely. If I'm not mistaken, even >>> Python's operators behave exactly like the equivalent C operators here, >>> right? And numeric operations would always return a double, so even >>> operations on doubles that involve integers would run completely in C space. >>> >>> Apart from a couple of further optimisations for methods and properties, I >>> think enabling type inference here would give us a rather warmly requested >>> feature. >>> >> This is now http://trac.cython.org/cython_trac/ticket/460. > > Rethinking this some more, there are a couple of problems with this, e.g. > overflow errors: > > >>> 2.0**1000000 > Traceback (most recent call last): > OverflowError: (34, 'Numerical result out of range') > > Moving this calculation to C space means we have to catch the error and > raise an exception for it.
I've tried to find information about this but what seems most likely to me now is that this is simply "defined by implementation" in current CPython. x**y and x / 0 will give exceptions, but + will give "inf" on overflow. Only real lead was this from 2000: http://mail.python.org/pipermail/python-dev/2000-May/003990.html """ Anyway, once all that is done, float overflow *will* raise an exception (by default; there will also be a way to turn that off), unlike what happens today. Before then, I guess continuing the current policy of benign neglect (i.e., let it overflow silently) is best for consistency. Without access to all the 754 features in C, it's not even easy to detect overflow now! """ I think this is orthogonal to type inference. I.e. like integers, floating point in Cython should have Python semantics by default: cdef double x = 2.0, y = 1000000.0 print x**y # exception, not "inf" With a directive to disable it, of course. -- Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
