-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Math.gcd new function #8331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Math.gcd new function #8331
Conversation
Edit: Not relevant anymore. |
Hello jbbjarnason, out of curiosity I had a look into this commit ( I want to learn how to contribute to MPy and the gcd topic seems simple). I tested the following algorithms:
gcd_iter() is from Rosetta code http://www.rosettacode.org/wiki/Greatest_common_divisor#C. Greetings, |
My next attempt: Is this the correct way to deal with potentially big integers? Can we compare them with ==, assign with =, compare against 0 with < 0 or do we need the binary ops in the latter cases?
|
@rkompass |
5dba62d
to
6ee337e
Compare
hello. In |
Hello you could test with gcd(1279980503569793028, 1234567844460). (=12). The computation with recursion seems o.k. in the one-liner case with %. |
I misunderstood the question, thought you referred to the gcd_iter(). The one-liner from rosetta-code works by recursion. Recursion may consume lots of memory (stack??) if one does not know it's limited. |
aeacf3b
to
fe9dcc2
Compare
fe9dcc2
to
eacfb85
Compare
After the update to Python 3.9 on Windows the error which is mentioned here: #8301 is happening. 3 tests failed: uasyncio_basic uasyncio_heaplock uasyncio_wait_task
--- "C:/projects/micropython/tests/results\\extmod_uasyncio_basic.py.exp" 2022-02-26 18:34:08.483553500 +0000
+++ "C:/projects/micropython/tests/results\\extmod_uasyncio_basic.py.out" 2022-02-26 18:34:08.483553500 +0000
@@ -3,4 +3,4 @@
short
long
negative
-took 20 40 0
+took 30 50 0
--- "C:/projects/micropython/tests/results\\extmod_uasyncio_heaplock.py.exp" 2022-02-26 18:34:07.087346800 +0000
+++ "C:/projects/micropython/tests/results\\extmod_uasyncio_heaplock.py.out" 2022-02-26 18:34:07.087346800 +0000
@@ -3,7 +3,7 @@
2 0
sleep
1 1
-1 2
2 1
+1 2
1 3
finish
--- "C:/projects/micropython/tests/results\\extmod_uasyncio_wait_task.py.exp" 2022-02-26 18:34:08.814773600 +0000
+++ "C:/projects/micropython/tests/results\\extmod_uasyncio_wait_task.py.out" 2022-02-26 18:34:08.814773600 +0000
@@ -5,6 +5,6 @@
start
hello
world
-took 40 40
+took 50 50
task_raise
ValueError |
It's unlikely that is related to Python 3.9: those tests are exectued using MicroPython, and also happen with other Python versions. |
86495b7
to
ce7a5fb
Compare
I'll try to do it next week. |
ce7a5fb
to
89241c4
Compare
Addition to Math Special. Greatest common divisor. Inputs are constrained to only integers. Implemented with variadic amount of arguments. Tests from CPython. Link: https://github.com/python/cpython/blob/main/Lib/test/test_math.py.
ports/windows/.appveyor: Update Windows virtual env. to 3.9. New Python features depend on more recent Python.
89241c4
to
a0bf6da
Compare
I have added tests from CPython covering the gcd function, which should bring confidence to these changes. |
@dpgeorge should I split this PR into two PR, one for the CPython update and another one for the math.gcd function? |
I'm just looking at this now, and have three comments/questions:
|
Yes, I think that's a good idea, see point (3) above. That will separate the discussion about CPython test version requirements. |
We don't have an explicit example of usage, but gcd is often used in network security and other math applications. It was not exposed to the user in MP so we decided to add it in, as it is referenced here #1329 as being not yet implemented.
We were not aware of it.
At that point in time we were not aware of Conclusion:Should we abandon this PR since we don't have an explicit use case? |
FWIW I've needed gcd more than once in numerical stuff, and then implemented it in Python with the typcial recursive method. |
Hi! |
Or also this one:
It is not recursive and is very efficient: it converges to the result in fewer cycles than using subtraction. |
I made a few benchmarks on C Python 3.8. About recursive functions on microcontrollers... IMHO they are a big disgrace. |
@massimosala if I remember correctly the current implementation of this PR is doing really similar to what you mention in C, https://github.com/micropython/micropython/pull/8331/files#diff-4eb012569ff9502b6617d7166eee7dc32340c2d1c737690a8226039b12c2efc5R222-R226 |
utilize CIRCUITPY_CONSOLE_UART_BAUDRATE parameter
This is an automated heads-up that we've just merged a Pull Request See #13763 A search suggests this PR might apply the STATIC macro to some C code. If it Although this is an automated message, feel free to @-reply to me directly if |
Implement the greatest common divisor function in the global math module. I found it best placed under the definition of
MICROPY_PY_MATH_SPECIAL_FUNCTIONS
please suggest if needed to place it elsewhere.The implementation covers the change in CPython 3.5 https://docs.python.org/3/whatsnew/3.5.html#math and as well as the more recent change in version 3.9 https://docs.python.org/3/whatsnew/3.9.html#math.
As a consequence the github pipelines need to run Python version 3.9, the image "ubuntu-latest (20.04)" is running on 3.8. So I added the necessary changes.However, those changes do have some side effect including problems with dependencies to elftools in the coverage pipelines. My first guess is that the pip library is installing elftools for Python 3.8 (as it is the default Python version in ubuntu-latest) which is not found while running Python 3.9. How would you like this handled?
Edit: not relevant anymore.
The Github Actions configuration files have been updated to use version 3.9. As well as the Windows port.
This was worked on with the assistance of @wang3450