summaryrefslogtreecommitdiffstatshomepage
path: root/py/modmath.c
Commit message (Collapse)AuthorAge
* py/obj: Fix nan handling in object REPR_C and REPR_D.Yoctopuce dev6 days
| | | | | | | | | | | | | | | | CPython math.nan is positive with regards to copysign. The signaling bit (aka sign flag) was incorrectly set. In addition, REPR_C and REPR_D should only use the _true_ nan to prevent system crash in case of hand-crafted floats. For instance, with REPR_C, any nan-like float following the pattern `01111111 1xxxxxxx xxxxxxxx xxxxx1xx` would be switched to an immediate object or a qstr string. When the qstr index is too large, this would cause a crash. This commit fixes the issue, and adds the relevant test cases. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
* all: Rename the "NORETURN" macro to "MP_NORETURN".Alessandro Gatti2025-05-13
| | | | | | | | | | | | | | | | This commit renames the NORETURN macro, indicating to the compiler that a function does not return, into MP_NORETURN to maintain the same naming convention of other similar macros. To maintain compaitiblity with existing code NORETURN is aliased to MP_NORETURN, but it is also deprecated for MicroPython v2. This changeset was created using a similar process to decf8e6a8bb940d5829ca3296790631fcece7b21 ("all: Remove the "STATIC" macro and just use "static" instead."), with no documentation or python scripts to change to reflect the new macro name. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/modmath: Add option to work around -inf bug in a port's tgamma.Angus Gratton2024-08-07
| | | | | | | | | This is needed for a workaround on esp32 port (in child commit), which produces incorrect results otherwise. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton2024-03-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The STATIC macro was introduced a very long time ago in commit d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was to have the option to define it to nothing so that all static functions become global functions and therefore visible to certain debug tools, so one could do function size comparison and other things. This STATIC feature is rarely (if ever) used. And with the use of LTO and heavy inline optimisation, analysing the size of individual functions when they are not static is not a good representation of the size of code when fully optimised. So the macro does not have much use and it's simpler to just remove it. Then you know exactly what it's doing. For example, newcomers don't have to learn what the STATIC macro is and why it exists. Reading the code is also less "loud" with a lowercase static. One other minor point in favour of removing it, is that it stops bugs with `STATIC inline`, which should always be `static inline`. Methodology for this commit was: 1) git ls-files | egrep '\.[ch]$' | \ xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/" 2) Do some manual cleanup in the diff by searching for the word STATIC in comments and changing those back. 3) "git-grep STATIC docs/", manually fixed those cases. 4) "rg -t python STATIC", manually fixed codegen lines that used STATIC. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/modmath: Fix two-argument math function domain check.Damien George2023-02-24
| | | | | | | | | | | | | | | | Prior to this fix, pow(1.5, inf) and pow(0.5, -inf) (among other things) would incorrectly raise a ValueError, because the result is inf with the first argument being finite. This commit fixes this by allowing the result to be infinite if the first or second (or both) argument is infinite. This fix doesn't affect the other three math functions that have two arguments: - atan2 never returns inf, so always fails isinf(ans) - copysign returns inf only if the first argument x is inf, so will never reach the isinf(y) check - fmod never returns inf, so always fails isinf(ans) Signed-off-by: Damien George <damien@micropython.org>
* all: Remove third argument to MP_REGISTER_MODULE.Damien George2022-06-02
| | | | | | | | It's no longer needed because this macro is now processed after preprocessing the source code via cpp (in the qstr extraction stage), which means unused MP_REGISTER_MODULE's are filtered out by the preprocessor. Signed-off-by: Damien George <damien@micropython.org>
* py: Make builtin modules use MP_REGISTER_MODULE.Jim Mussared2022-05-18
| | | | Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/modmath: Add math.tau, math.nan and math.inf constants.stijn2022-01-23
| | | | Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
* py/modmath: Simplify handling of positional args to reduce code size.Damien George2020-12-14
| | | | | | | As a general pattern, required positional arguments that are not named do not need to be parsed using mp_arg_parse_all(). Signed-off-by: Damien George <damien@micropython.org>
* py, extmod: Add explicit initializers for default values.Emil Renner Berthing2020-10-22
| | | | | | | | | When compiling with -Wextra which includes -Wmissing-field-initializers GCC will warn that the defval field of mp_arg_val_t is not initialized. This is just a warning as it is defined to be zero initialized, but since it is a union it makes sense to be explicit about which member we're going to use, so add the explicit initializers and get rid of the warning.
* py: Fix handling of NaN in certain pow implementations.stijn2020-09-11
| | | | | Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly.
* py/modmath: Work around msvc float bugs in atan2, fmod and modf.stijn2020-05-28
| | | | | | Older implementations deal with infinity/negative zero incorrectly. This commit adds generic fixes that can be enabled by any port that needs them, along with new tests cases.
* all: Format code to add space after C++-style comment start.stijn2020-04-23
| | | | | | Note: the uncrustify configuration is explicitly set to 'add' instead of 'force' in order not to alter the comments which use extra spaces after // as a means of indenting text for clarity.
* all: Use MP_ERROR_TEXT for all error messages.Jim Mussared2020-04-05
|
* all: Convert exceptions to use mp_raise_XXX helpers in remaining places.Damien George2020-03-18
|
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* py/modmath: Implement math.isclose() for non-complex numbers.stijn2019-08-17
| | | | | | As per PEP 485, this function appeared in for Python 3.5. Configured via MICROPY_PY_MATH_ISCLOSE which is disabled by default, but enabled for the ports which already have MICROPY_PY_MATH_SPECIAL_FUNCTIONS enabled.
* py/modmath: Add math.factorial, optimised and non-opt implementations.Christopher Swenson2018-09-26
| | | | | | | | | | | | | | | 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.
* py: Shorten error messages by using contractions and some rewording.Damien George2018-09-20
|
* py/modmath: Convert log2 macro into a function.Damien George2017-10-10
| | | | | | So that a pointer to it can be passed as a pointer to math_generic_1. This patch also makes the function work for single and double precision floating point.
* py/modmath: Add full checks for math domain errors.Damien George2017-10-10
| | | | | | | | | | | | | | | | | | | | | This patch changes how most of the plain math functions are implemented: there are now two generic math wrapper functions that take a pointer to a math function (like sin, cos) and perform the necessary conversion to and from MicroPython types. This helps to reduce code size. The generic functions can also check for math domain errors in a generic way, by testing if the result is NaN or infinity combined with finite inputs. The result is that, with this patch, all math functions now have full domain error checking (even gamma and lgamma) and code size has decreased for most ports. Code size changes in bytes for those with the math module are: unix x64: -432 unix nanbox: -792 stm32: -88 esp8266: +12 Tests are also added to check domain errors are handled correctly.
* py: Change obsolete "///" comment formatting to normal comments.Damien George2017-08-30
| | | | | This comment style is no longer used because the docs are written by hand, not generated.
* all: Use the name MicroPython consistently in commentsAlexander Steffen2017-07-31
| | | | | There were several different spellings of MicroPython present in comments, when there should be only one.
* py/modmath: Check for zero division in log with 2 args.Damien George2017-07-04
|
* all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers.Damien George2017-06-15
|
* py/modmath: Allow trunc/ceil/floor to return a big int if necessary.Damien George2017-03-23
| | | | | | | Previous to this patch, if the result of the trunc/ceil/floor functions overflowed a small int, or was inf or nan, then a garbage value was returned. With this patch the correct big-int is returned if necessary, and exceptions are raised for inf or nan.
* py: Add MICROPY_FLOAT_CONST macro for defining float constants.Damien George2016-11-03
| | | | | | All float constants in the core should use this macro to prevent unnecessary creation of double-precision floats, which makes code less efficient.
* all: Remove 'name' member from mp_obj_module_t struct.Damien George2016-09-22
| | | | One can instead lookup __name__ in the modules dict to get the value.
* py/objfloat, py/modmath: Ensure M_PI and M_E defined.Colin Hogben2016-05-12
| | | | | | In some compliation enviroments (e.g. mbed online compiler) with strict standards compliance, <math.h> does not define constants such as M_PI. Provide fallback definitions of M_E and M_PI where needed.
* py: Change type signature of builtin funs that take variable or kw args.Damien George2016-01-11
| | | | | With this patch the n_args parameter is changed type from mp_uint_t to size_t.
* py/modmath: Add domain error checking to sqrt, log, log2, log10.Michael Buesch2015-12-12
| | | | | These functions will raise 'ValueError: math domain error' on invalid input.
* py: Add MP_ROM_* macros and mp_rom_* types and use them.Damien George2015-11-29
|
* py/modmath: Make log2, log10 and hyperbolic funcs be SPECIAL_FUNCTIONS.Damien George2015-11-17
| | | | | | Will be included only when MICROPY_PY_MATH_SPECIAL_FUNCTIONS is enabled. Also covers cmath module (but only log10 is there at the moment).
* py/modmath: Don't create symbol entry for expm1() if not needed.Paul Sokolovsky2015-11-14
|
* py/modmath: Make expm1() be in MICROPY_PY_MATH_SPECIAL_FUNCTIONS.Paul Sokolovsky2015-11-14
|
* unix/mpconfigport: Move log2() definition to modmath.c.Paul Sokolovsky2015-11-13
| | | | | It's safer to define it where it's used, defining it for all source files may lead to hard to diagnose conflicts in corner cases.
* py: Move float e/pi consts to objfloat and make mp_obj_float_t private.Damien George2015-10-20
|
* py: Rename MP_BOOL() to mp_obj_new_bool() for consistency in naming.Paul Sokolovsky2015-10-11
|
* py: Implement second arg for math.log (optional value for base).Damien George2015-06-13
|
* py: Make math special functions configurable and disabled by default.Damien George2015-02-22
| | | | | | | The implementation of these functions is very large (order 4k) and they are rarely used, so we don't enable them by default. They are however enabled in stmhal and unix, since we have the room.
* py: Move to guarded includes, everywhere in py/ core.Damien George2015-01-01
| | | | Addresses issue #1022.
* py: Make functions static where appropriate.Damien George2014-12-10
|
* Use MP_DEFINE_CONST_DICT macro to define module dicts.Damien George2014-11-29
| | | | | This is just a clean-up of the code. Generated code is exactly the same.
* doc: Document gc, sys, math, cmath.Damien George2014-08-10
|
* Rename machine_(u)int_t to mp_(u)int_t.Damien George2014-07-03
| | | | See discussion in issue #50.
* py: Include mpconfig.h before all other includes.Paul Sokolovsky2014-06-21
| | | | | | It defines types used by all other headers. Fixes #691.
* Prefix ARRAY_SIZE with micropython prefix MP_Emmanuel Blot2014-06-19
|
* Rename bultins config variables to MICROPY_PY_BUILTINS_*.Damien George2014-06-01
| | | | | | | | | | This renames: MICROPY_PY_FROZENSET -> MICROPY_PY_BUILTINS_FROZENSET MICROPY_PY_PROPERTY -> MICROPY_PY_BUILTINS_PROPERTY MICROPY_PY_SLICE -> MICROPY_PY_BUILTINS_SLICE MICROPY_ENABLE_FLOAT -> MICROPY_PY_BUILTINS_FLOAT See issue #35 for discussion.
* Rename configuration variables controling Python features.Damien George2014-05-24
| | | | Now of the form MICROPY_PY_*. See issue #35.
* Fix some unused variables, and silence a clang warning about initialization ↵Antonin ENFRUN2014-05-12
| | | | override in vmentrytable.h