I participated discussion about complex numbers and did some experiments in Python. I do not understand this behaviour (same behavious in 2.7.2 and 3.2.2):

>>> -(0+1j)
(-0-1j)
>>> -(0+1j)==-1j
True
>>> -(0+1j)+0
-1j
>>> -(0+1j)-0
(-0-1j)
>>>

EDIT: Further investigation shows that there is -0.0 in Python, is this property of IEEE floating point arithmetic?

>>> -(1j)
(-0-1j)
>>> -(1j).real
-0.0
>>> -0.0
-0.0
>>> -0.0==0
True
>>> -0.0 is 0.0
False
>>> 0.0 is 0.0
True
>>> -0 is 0
True
>>>

And further it's id seems to change in each instance unlike 0.0:

>>> id(-0.0)
12428376
>>> id(0.0)
12428456
>>> id(-0.0)
12428360
>>> id(0.0)
12428456
>>>

The -0.0 exists in the ieee 754 representation, it's zero with the sign bit set

>>> from binascii import hexlify
>>> import struct
>>> def ieee754(x):
...  p = struct.pack("d", x)
...  s = bin(int(b"1" + hexlify(p), 16))[3:]
...  return " ".join(reversed([s[i:i+8] for i in xrange(0, len(s), 8)]))
... 
>>> ieee754(0.0)
'00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000'
>>> ieee754(-0.0)
'10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.