Post Reply 
complex numbers in Python
10-12-2024, 01:18 PM
Post: #1
complex numbers in Python
In Python on my computer,
Code:
complex("1.2+3.4j")
complex("1.2-3.4j")
return
Code:
(1.2+3.4j)
(1.2-3.4j)
as expected.

However, in Python on the Prime, these give a "ValueError: invalid syntax for number" error message.

Interestingly,
Code:
complex("1.2")
complex("3.4j")
correctly return
Code:
(1.2+0j)
(3.4j)

Even
Code:
complex("1.2+0j")
complex("(1.2+0j)")
both fail.

Code:
complex("1.2")+complex("3.4j")
correctly returns
Code:
(1.2+3.4j)
but I'd rather not have to do that sort of processing unnecessarily.

Is this a microPython issue or a Prime issue? Or am I doing something completely wrong?
Find all posts by this user
Quote this message in a reply
10-12-2024, 02:25 PM
Post: #2
RE: complex numbers in Python
Looking through the MicroPython tests, it seems like this should work:
https://github.com/micropython/micropyth...omplex1.py

However, these tests might refer to a newer version of MicroPython than the one implemented in the HP Prime.

I'm not sure what your exact requirements are, but there are other methods to achieve the same result. For example, you can pass the real and imaginary values as separate arguments:
Code:
complex(1.2, 3.4)
If the input has to be a string, you can use eval:
Code:
eval("1.2+3.4j")

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
10-12-2024, 05:51 PM
Post: #3
RE: complex numbers in Python
(10-12-2024 02:25 PM)komame Wrote:  I'm not sure what your exact requirements are, but there are other methods to achieve the same result.

I'm trying to pass arguments from PPL to Python via the argv[] array of strings.

(10-12-2024 02:25 PM)komame Wrote:  If the input has to be a string, you can use eval:
Code:
eval("1.2+3.4j")

Say, that will work nicely. Thanks.
Find all posts by this user
Quote this message in a reply
10-12-2024, 06:04 PM
Post: #4
RE: complex numbers in Python
Here's something interesting.

Python on NumWorks has the same behavior as the Prime: complex("1.2") and complex("3.4j") both work, but complex("1.2+3.4j") fails with the exact same error message as the Prime. This would lead me to think that this is a microPython issue.

However, on both the TI 84+ CE Python Edition and on the TI Nspire CX II CAS, complex("1.2+3.4j") returns (1.2+3.4j) as expected. This would lead me to think that the issue is related to the implementation.

NumWorks does not recognize "import sys", but on the TI 84, sys.implementation returns (name='tipython', version=(3, 2, 4, 2)). On the TI Nspire, it returns (name='micropython', version=(1, 11, 0))

Since the Prime is using version=(1, 9, 4), perhaps this issue was fixed with the later (1, 11, 0). Or, maybe TI patched it for their own use.
Find all posts by this user
Quote this message in a reply
10-12-2024, 06:34 PM
Post: #5
RE: complex numbers in Python
and using the online interpreter at https://micropython.org/unicorn/
where sys.implementation returns (name='micropython', version=(1, 19, 1), _machine='unicorn with Cortex-M3'), complex("1.2+3.4j") also fails with ValueError: invalid syntax for number

Since this is later than the TI Nspire version, it suggests that TI may have patched their version.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)