Python: sign of nothing

2007-07-01

Whilst writing a rather longer article related to floating point, and having recently been sensitized to these sorts of issues, I note the following behaviour:

>>> x=-0.0;y=0.0;print x,y
-0.0 -0.0

To my mind this is beyond bonkers, but I’ve been convinced by others before that behaviour I used to think was bonkers may in fact be sensible. The behaviour is obviously caused by the compiler caching the constant values.

Anyone care to defend it?

About these ads

4 Responses to “Python: sign of nothing”

  1. Gareth Rees Says:

    It’s clearly a bug but hard to fix portably in CPython; see this comp.lang.python thread. A couple of posters in that thread point out that the disassembler is useful in figuring out what’s going on:

    >>> c = compile(‘(0.0,-0.0)’, ‘eval’, ‘eval’)
    >>> import dis
    >>> dis.dis(c)
    1 0 LOAD_CONST 1 ((0.0, 0.0))
    3 RETURN_VALUE

  2. drj11 Says:

    Perhaps it is difficult, but it’s patched. Looks like the patch wasn’t made by the “in-crowd” though. Wonder if it’ll make it?

  3. Paul Says:

    I don’t know if the patch will make it, but the author of the patch is most certainly in the in-crowd.

  4. drj11 Says:

    In-crowd: Oops. My misreading of an unfamiliar scene.


Comments are closed.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: