summaryrefslogtreecommitdiff
path: root/py/modmath.c
AgeCommit message (Collapse)Author
2025-06-24py/obj: Fix nan handling in object REPR_C and REPR_D.Yoctopuce dev
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>
2025-05-13all: Rename the "NORETURN" macro to "MP_NORETURN".Alessandro Gatti
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>
2024-08-07py/modmath: Add option to work around -inf bug in a port's tgamma.Angus Gratton
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>
2024-03-07all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton
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>
2023-02-24py/modmath: Fix two-argument math function domain check.Damien George
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>
2022-06-02all: Remove third argument to MP_REGISTER_MODULE.Damien George
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>
2022-05-18py: Make builtin modules use MP_REGISTER_MODULE.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-01-23py/modmath: Add math.tau, math.nan and math.inf constants.stijn
Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
2020-12-14py/modmath: Simplify handling of positional args to reduce code size.Damien George
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>
2020-10-22py, extmod: Add explicit initializers for default values.Emil Renner Berthing
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.
2020-09-11py: Fix handling of NaN in certain pow implementations.stijn
Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly.
2020-05-28py/modmath: Work around msvc float bugs in atan2, fmod and modf.stijn
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.
2020-04-23all: Format code to add space after C++-style comment start.stijn
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.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-03-18all: Convert exceptions to use mp_raise_XXX helpers in remaining places.Damien George
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2019-08-17py/modmath: Implement math.isclose() for non-complex numbers.stijn
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.
2018-09-26py/modmath: Add math.factorial, optimised and non-opt implementations.Christopher Swenson
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.
2018-09-20py: Shorten error messages by using contractions and some rewording.Damien George
2017-10-10py/modmath: Convert log2 macro into a function.Damien George
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.
2017-10-10py/modmath: Add full checks for math domain errors.Damien George
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.
2017-08-30py: Change obsolete "///" comment formatting to normal comments.Damien George
This comment style is no longer used because the docs are written by hand, not generated.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-07-04py/modmath: Check for zero division in log with 2 args.Damien George
2017-06-15all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers.Damien George
2017-03-23py/modmath: Allow trunc/ceil/floor to return a big int if necessary.Damien George
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.
2016-11-03py: Add MICROPY_FLOAT_CONST macro for defining float constants.Damien George
All float constants in the core should use this macro to prevent unnecessary creation of double-precision floats, which makes code less efficient.
2016-09-22all: Remove 'name' member from mp_obj_module_t struct.Damien George
One can instead lookup __name__ in the modules dict to get the value.
2016-05-12py/objfloat, py/modmath: Ensure M_PI and M_E defined.Colin Hogben
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.
2016-01-11py: Change type signature of builtin funs that take variable or kw args.Damien George
With this patch the n_args parameter is changed type from mp_uint_t to size_t.
2015-12-12py/modmath: Add domain error checking to sqrt, log, log2, log10.Michael Buesch
These functions will raise 'ValueError: math domain error' on invalid input.
2015-11-29py: Add MP_ROM_* macros and mp_rom_* types and use them.Damien George
2015-11-17py/modmath: Make log2, log10 and hyperbolic funcs be SPECIAL_FUNCTIONS.Damien George
Will be included only when MICROPY_PY_MATH_SPECIAL_FUNCTIONS is enabled. Also covers cmath module (but only log10 is there at the moment).
2015-11-14py/modmath: Don't create symbol entry for expm1() if not needed.Paul Sokolovsky
2015-11-14py/modmath: Make expm1() be in MICROPY_PY_MATH_SPECIAL_FUNCTIONS.Paul Sokolovsky
2015-11-13unix/mpconfigport: Move log2() definition to modmath.c.Paul Sokolovsky
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.
2015-10-20py: Move float e/pi consts to objfloat and make mp_obj_float_t private.Damien George
2015-10-11py: Rename MP_BOOL() to mp_obj_new_bool() for consistency in naming.Paul Sokolovsky
2015-06-13py: Implement second arg for math.log (optional value for base).Damien George
2015-02-22py: Make math special functions configurable and disabled by default.Damien George
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.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-12-10py: Make functions static where appropriate.Damien George
2014-11-29Use MP_DEFINE_CONST_DICT macro to define module dicts.Damien George
This is just a clean-up of the code. Generated code is exactly the same.
2014-08-10doc: Document gc, sys, math, cmath.Damien George
2014-07-03Rename machine_(u)int_t to mp_(u)int_t.Damien George
See discussion in issue #50.
2014-06-21py: Include mpconfig.h before all other includes.Paul Sokolovsky
It defines types used by all other headers. Fixes #691.
2014-06-19Prefix ARRAY_SIZE with micropython prefix MP_Emmanuel Blot
2014-06-01Rename bultins config variables to MICROPY_PY_BUILTINS_*.Damien George
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.
2014-05-24Rename configuration variables controling Python features.Damien George
Now of the form MICROPY_PY_*. See issue #35.
2014-05-12Fix some unused variables, and silence a clang warning about initialization ↵Antonin ENFRUN
override in vmentrytable.h