kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

Complex Numbers #78

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to see support for complex numbers (like 25+8j and such).

Original issue reported on code.google.com by mikda...@yahoo.com on 1 Feb 2010 at 7:28

GoogleCodeExporter commented 9 years ago
Hi, I'm not sure what you mean by that - could you elaborate? There isn't a 
natural
representation of complex numbers as binary strings (that I'm aware of), so the 
best
you can do is use a pair of floats (or integers):

>>> c_format = 'float:64, float:64' # store as pair of floats
>>> c = 25+8j
>>> s = bitstring.pack(c_format, c.real, c.imag)
>>> d = complex(*s.unpack(c_format))

This is a fairly specialised need so unless you can persuade me otherwise I 
don't
plan to add any explicit support. Thanks.

Original comment by python.bitstring@googlemail.com on 1 Feb 2010 at 11:47

GoogleCodeExporter commented 9 years ago
Well, I'm trying to test the performance of complex numbers vs. 2-tuples vs. 
2-lists
(like the memory they take up, the time it takes to assign/change, etc.) and I 
was
using BitString to compare the size in memory, but I couldn't figure out how to 
get
the binary representations from them to see how big they were.

I guess that's not a very good persuasion, unless complex numbers have some huge
memory or performance boost that would make even knowing about them worth it.

Original comment by mikda...@yahoo.com on 3 Feb 2010 at 7:21

GoogleCodeExporter commented 9 years ago
I'm not sure it makes much sense to use bitstring to measure memory usage of 
types -
you need to specify the type yourself so you don't gain any information. As to 
the
size of complex:

>>> type(c.real)
<type 'float'>

So they consist of a pair of floats, and floats are 64-bit in Python. If your 
complex
numbers are always integers then you might be better memory-wise using a custom 
tuple
(though only for 32-bit Python, and not if it's a 64-bit Python build, as then 
the
ints will be 64-bit as well!)

Original comment by python.bitstring@googlemail.com on 3 Feb 2010 at 8:30