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") Code: (1.2+3.4j) However, in Python on the Prime, these give a "ValueError: invalid syntax for number" error message. Interestingly, Code: complex("1.2") Code: (1.2+0j) Even Code: complex("1.2+0j") Code: complex("1.2")+complex("3.4j") Code: (1.2+3.4j) Is this a microPython issue or a Prime issue? Or am I doing something completely wrong? |
|||
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) Code: eval("1.2+3.4j") Piotr Kowalewski |
|||
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: Say, that will work nicely. Thanks. |
|||
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. |
|||
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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)