diff options
author | Christopher Swenson <chris@caswenson.com> | 2018-08-27 10:32:21 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-09-26 15:03:04 +1000 |
commit | 8c656754aa2892cbd36968bfaab1ff7033edeb0f (patch) | |
tree | 30eab37cc6b51eb9fb3785183b8543a25abbb4b8 /tests/float/math_factorial_intbig.py | |
parent | 7b452e7466d7fc4058eab5eb60bbbbd125039d88 (diff) | |
download | micropython-8c656754aa2892cbd36968bfaab1ff7033edeb0f.tar.gz micropython-8c656754aa2892cbd36968bfaab1ff7033edeb0f.zip |
py/modmath: Add math.factorial, optimised and non-opt implementations.
This commit adds the math.factorial function in two variants:
- squared difference, which is faster than the naive version, relatively
compact, and non-recursive;
- a mildly optimised recursive version, faster than the above one.
There are some more optimisations that could be done, but they tend to take
more code, and more storage space. The recursive version seems like a
sensible compromise.
The new function is disabled by default, and uses the non-optimised version
by default if it is enabled. The options are MICROPY_PY_MATH_FACTORIAL
and MICROPY_OPT_MATH_FACTORIAL.
Diffstat (limited to 'tests/float/math_factorial_intbig.py')
-rw-r--r-- | tests/float/math_factorial_intbig.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/tests/float/math_factorial_intbig.py b/tests/float/math_factorial_intbig.py new file mode 100644 index 0000000000..19d853df2a --- /dev/null +++ b/tests/float/math_factorial_intbig.py @@ -0,0 +1,14 @@ +try: + import math + math.factorial +except (ImportError, AttributeError): + print('SKIP') + raise SystemExit + + +for fun in (math.factorial,): + for x in range(-1, 30): + try: + print('%d' % fun(x)) + except ValueError as e: + print('ValueError') |