Complex evaluation bug

B

Brian Blazer

I am not exactly sure what is going on, but I get the error:

ValueError: complex() arg is a malformed string

I think that it might be because the value of 'j' is not defined.

But I am a newbie so I could very well be wrong.

Brian Blazer
(e-mail address removed)
 
G

Gary Herron

of said:
a = 1+3j
complex(str(a))

Why does this not work ? It should
Says who?

By normal conventions in Python, "str" attempts only to make a "nice"
human readable representation. The function "repr" is usually expected
to provide output that can be parsed back into the original object.
(Although for the numeric complex type the two produce identical results.)

Further, constructors are rarely expected to parse a string
representation to return an object. The function "eval" is usually
expected to provide that functionality.

So, putting them together, you could expect
eval(repr(a))
to reproduce a, and in fact it does so.

Gary Herron
 
W

wittempj

py> c = complex(1, 3)
py> print c
(1+3j)
py> d = complex('1+3j')
py> print d
(1+3j)
py> str(1+3j)
'(1+3j)'


complex takes two numbers, or a string representing a complex number.
the string you supply isn't a representation of valid complex number.
 
M

Mel Wilson

of said:
a = 1+3j
complex(str(a))

Why does this not work ? It should

It would be nice.
Looks like str(1+3j) is returning an expression in string
form. Maybe there is no actual complex literal.

eval (str(1+3j)) works.

Python 2.4.2 (#1, Jan 23 2006, 21:24:54) [GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more
information.Traceback (most recent call last):
(-8+6j)
 
P

Paul McGuire

Mel Wilson said:
It would be nice.
Looks like str(1+3j) is returning an expression in string
form. Maybe there is no actual complex literal.

The problem is that str(1+3j) emits a string enclosed in parens. Stripping
them off makes the string acceptable to complex's constructor, without
invoking eval.
Traceback (most recent call last):
File said:
complex(str(a)[1:-1]) (1+3j)
complex(str(a).strip("()"))
(1+3j)


-- Paul
 
C

Christophe

Gary Herron a écrit :
Says who?
By normal conventions in Python, "str" attempts only to make a "nice"
human readable representation. The function "repr" is usually expected
to provide output that can be parsed back into the original object.
(Although for the numeric complex type the two produce identical results.)

Further, constructors are rarely expected to parse a string
representation to return an object. The function "eval" is usually
expected to provide that functionality.

So, putting them together, you could expect
eval(repr(a))
to reproduce a, and in fact it does so.

Says who ?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
 
F

Fredrik Lundh

Christophe said:
So, putting them together, you could expect
eval(repr(a))
to reproduce a, and in fact it does so.

Says who ?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

in what language is "eval" spelled "complex" ?

</F>
 
C

Christophe

Fredrik Lundh a écrit :
Christophe said:
So, putting them together, you could expect
eval(repr(a))
to reproduce a, and in fact it does so.


Says who ?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
repr(1+3j) '(1+3j)'
complex(repr(1+3j))
Traceback (most recent call last):


in what language is "eval" spelled "complex" ?

</F>

Oups, I was too fast to read what was written. I though you only changed
one of the terms ( str -> repr ).

You'll note that repr and str produce the exact same result for complex.
And also, I'm not sure using eval anywhere is a good idea so it probably
doesn't help for what the OP wants to do.
 
P

Paul McGuire

Oups, I was too fast to read what was written. I though you only changed
one of the terms ( str -> repr ).

You'll note that repr and str produce the exact same result for complex.
And also, I'm not sure using eval anywhere is a good idea so it probably
doesn't help for what the OP wants to do.

An eval-less approach - the problem is the enclosing parens.
Traceback (most recent call last):
File said:
complex(str(a)[1:-1]) (1+3j)
complex(str(a).strip("()"))
(1+3j)

-- Paul
 
H

Heiko Wundram

Am Freitag 19 Mai 2006 18:03 schrieb Paul McGuire:
An eval-less approach - the problem is the enclosing parens.
<snip>

I've just submitted two patches to the Python bugtracker at:

http://sourceforge.net/tracker/index.php?func=detail&aid=1491866&group_id=5470&atid=305470

which either change the repr() format (removing the parentheses), which I find
doubtful, because it's not backwards-compatible, or alter the constructor to
accept the repr() format for complex numbers (a bracketed number).

Feel free to comment.

--- Heiko.
 
O

of

Heiko said:
Am Freitag 19 Mai 2006 18:03 schrieb Paul McGuire:



I've just submitted two patches to the Python bugtracker at:

http://sourceforge.net/tracker/index.php?func=detail&aid=1491866&group_id=5470&atid=305470

which either change the repr() format (removing the parentheses), which I find
doubtful, because it's not backwards-compatible, or alter the constructor to
accept the repr() format for complex numbers (a bracketed number).

Feel free to comment.

--- Heiko.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top