HP Forums
complex numbers in Python - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: complex numbers in Python (/thread-22491.html)



complex numbers in Python - Wes Loewer - 10-12-2024 01:18 PM

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?


RE: complex numbers in Python - komame - 10-12-2024 02:25 PM

Looking through the MicroPython tests, it seems like this should work:
https://github.com/micropython/micropython/blob/master/tests/float/complex1.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")



RE: complex numbers in Python - Wes Loewer - 10-12-2024 05:51 PM

(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.


RE: complex numbers in Python - Wes Loewer - 10-12-2024 06:04 PM

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.


RE: complex numbers in Python - Wes Loewer - 10-12-2024 06:34 PM

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.