[Pyrex] is this a bug?
Robin Becker
robin at reportlab.com
Sun Nov 25 13:43:20 CET 2007
Robin Becker wrote:
> I find in my learning project that the following code doesn't behave as
> expected in a python method
>
> def compute_breakpoints(self,
> line_lengths,
> double looseness = 0,
> double tolerance = 1,
> double fitness_demerit = 100,
> double flagged_demerit = 100,
> ):
> cdef double r
>
> r = ......
>
> if -1 <= r <= tolerance:
> ......
>
> r was printed out as -1.41176470588, but the if test succeeded. Correct
> behaviour was restored if either the double was removed from the
> tolerance argument or the test was rewritten as
>
> if -1.0 <= r <= tolerance:
I looked into this and it seems that for r and tolerance declared as double
if -1. <= r <= tolerance:
gets translated to
/* "C:\code\users\robin\_tex_wrap_rgb.pyx":339 */
__pyx_1 = (-1.) <= __pyx_v_r;
if (__pyx_1) {
__pyx_1 = __pyx_v_r <= __pyx_v_tolerance;
}
if (__pyx_1) {
.....
whereas
if -1 <= r <= tolerance:
becomes
/* "C:\code\users\robin\_tex_wrap_rgb.pyx":339 */
__pyx_1 = (-1) <= ((long)__pyx_v_r);
if (__pyx_1) {
__pyx_1 = __pyx_v_r <= __pyx_v_tolerance;
}
if (__pyx_1) {
ie it seems as though the left hand operand is being used as the type
specifier. That means that (-1) <= ((long)-1.4) is presumably evaluating
as (-1) <= (-1). I expected that the second case for the pair
(long,double) we would get
__pyx_1 = ((double)(-1)) <= __pyx_v_r;
however, I don't know whether the pyrex parser has enough information to
do these tests in the general case(in this case it could, but doesn't).
If the correct type attributions cannot be made, wouldn't it be better
to convert both operands to python variables? If we're going for
optimisation could these unsafe operations be warned about?
--
Robin Becker
More information about the Pyrex
mailing list