summaryrefslogtreecommitdiffstatshomepage
path: root/tests/float
Commit message (Collapse)AuthorAge
* py/parsenum: Fix parsing complex literals with negative real part.Jeff Epler11 days
| | | | | | | | | | | If a complex literal had a negative real part and a positive imaginary part, it was not parsed properly because the imaginary part also came out negative. Includes a test of complex parsing, which fails without this fix. Co-authored-by: ComplexSymbol <141301057+ComplexSymbol@users.noreply.github.com> Signed-off-by: Jeff Epler <jepler@gmail.com>
* tests/float/math_constants.py: Test actual e and pi constant values.Damien George2025-05-22
| | | | | | | | | | | | | The existing test for `math.e` and `math.pi` constants can fail on certain targets if the functions `math.exp()` and/or `math.cos()` are not accurate enough (eg out by an LSB of float precision). For example this test currently fails on PYBD_SF6 which uses double precision floats (and that's due to the `lib/libm_dbl/exp.c` implementation not being exact). This commit changes this constant test so that it tests the actual constant value, not the evaluation of `exp()` and `cos()` functions. Signed-off-by: Damien George <damien@micropython.org>
* py/parsenum: Reduce code footprint of mp_parse_num_float.Yoctopuce dev2025-02-28
| | | | | | | | | | | | | The mantissa parsing code uses a floating point variable to accumulate digits. Using an `mp_float_uint_t` variable instead and casting to `mp_float_t` at the very end reduces code size. In some cases, it also improves the rounding behaviour as extra digits are taken into account by the int-to-float conversion code. An extra test case handles the special case where mantissa overflow occurs while processing deferred trailing zeros. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
* qemu: Rename qemu-arm port to qemu.Damien George2024-09-06
| | | | | | Because this port now supports multiple architectures. Signed-off-by: Damien George <damien@micropython.org>
* tests/float: Use "not" instead of ~ to invert bool value.Damien George2024-05-28
| | | | | | | | | Otherwise CPython gives a deprecation warning. This test is not actually testing inversion of bools, rather that bit of the test is used to compute the pass/fail result. Signed-off-by: Damien George <damien@micropython.org>
* tests/float/float_struct_e.py: Add specific test for struct 'e' type.Damien George2024-03-20
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/binary: Support half-float 'e' format in struct pack/unpack.Matthias Urlichs2024-03-20
| | | | | | | | | This commit implements the 'e' half-float format: 10-bit mantissa, 5-bit exponent. It uses native _Float16 if supported by the compiler, otherwise uses custom bitshifting encoding/decoding routines. Signed-off-by: Matthias Urlichs <matthias@urlichs.de> Signed-off-by: Damien George <damien@micropython.org>
* tests/float/inf_nan_arith.py: Include -inf in argument combos.Damien George2023-12-06
| | | | | | This adds tests for, eg, -inf + inf which should be nan. Signed-off-by: Damien George <damien@micropython.org>
* tests/float/math_domain.py: Tweak test to also pass with obj-repr-C.Damien George2023-09-29
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/float/float_format_ints.py: Put power-of-10 test in separate file.Damien George2023-09-29
| | | | | | This test doesn't pass on builds with 30-bit floats (object repr C). Signed-off-by: Damien George <damien@micropython.org>
* tests/float: Test domain errors for more combos of args to math funcs.Damien George2023-06-18
| | | | | | | | Instead of having a special set of arguments to test for each math-module function, just test all functions with all sets of arguments. This gives improved test cases to prevent regressions. Signed-off-by: Damien George <damien@micropython.org>
* tests: Replace umodule with module everywhere.Jim Mussared2023-06-08
| | | | | | This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* 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>
* tests/float: Make output of math function tests more readable.Damien George2023-02-16
| | | | | | By explicitly naming the function, its arguments, and result. Signed-off-by: Damien George <damien@micropython.org>
* tests/float: Add domain checks for log and also -inf.Damien George2023-02-16
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/float: Skip new complex tests if complex unavailable.Damien George2023-02-09
| | | | | | These complex tests were recently added. Signed-off-by: Damien George <damien@micropython.org>
* py/formatfloat: Use pow(10, e) instead of pos/neg_pow lookup tables.Dan Ellis2022-08-12
| | | | | | | | | | | | | Rework the conversion of floats to decimal strings so it aligns precisely with the conversion of strings to floats in parsenum.c. This is to avoid rendering 1eX as 9.99999eX-1 etc. This is achieved by removing the power- of-10 tables and using pow() to compute the exponent directly, and that's done efficiently by first estimating the power-of-10 exponent from the power-of-2 exponent in the floating-point representation. Code size is reduced by roughly 100 to 200 bytes by this commit. Signed-off-by: Dan Ellis <dan.ellis@gmail.com>
* py/formatfloat: Format all whole-number floats exactly.Dan Ellis2022-07-26
| | | | | | | | | | | | | Formerly, py/formatfloat would print whole numbers inaccurately with nonzero digits beyond the decimal place. This resulted from its strategy of successive scaling of the argument by 0.1 which cannot be exactly represented in floating point. The change in this commit avoids scaling until the value is smaller than 1, so all whole numbers print with zero fractional part. Fixes issue #4212. Signed-off-by: Dan Ellis dan.ellis@gmail.com
* py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first.Damien George2022-07-25
| | | | | | | | | | | | | | | | | | This commit simplifies mp_obj_get_complex_maybe() by first calling mp_obj_get_float_maybe() to handle the cases corresponding to floats. Only if that fails does it attempt to extra a full complex number. This reduces code size and also means that mp_obj_get_complex_maybe() now supports user-defined classes defining __float__; in particular this allows user-defined classes to be used as arguments to cmath-module function. Furthermore, complex_make_new() can now be simplified to directly call mp_obj_get_complex(), instead of mp_obj_get_complex_maybe() followed by mp_obj_get_float(). This also improves error messages from complex with an invalid argument, it now raises "can't convert <type> to complex" rather than "can't convert <type> to float". Signed-off-by: Damien George <damien@micropython.org>
* py/obj: Add support for __float__ and __complex__ functions.Andrew Leech2022-07-25
|
* py/parsenum: Fix parsing of complex "j" and also "nanj", "infj".Damien George2022-06-23
| | | | | | | | | Prior to this commit, complex("j") would return 0j, and complex("nanj") would return nan+0j. This commit makes sure "j" is tested for after parsing the number (nan, inf or a decimal), and also supports the case of "j" on its own. Signed-off-by: Damien George <damien@micropython.org>
* py/parsenum: Support parsing complex numbers of the form "a+bj".Jim Mussared2022-06-23
| | | | | | To conform with CPython. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* all: Update Python formatting to latest Black version 22.1.0.Damien George2022-02-02
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/modmath: Add math.tau, math.nan and math.inf constants.stijn2022-01-23
| | | | Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
* tests/float: Make bytes/bytearray construct tests work with obj repr C.Damien George2021-06-18
| | | | | | | 2.5 can be represented correctly in object representation C, but 2.3 cannot (it is slightly truncated). Signed-off-by: Damien George <damien@micropython.org>
* tests: Make float and framebuf tests skip or run on big-endian archs.Damien George2021-05-26
| | | | Signed-off-by: Damien George <damien@micropython.org>
* 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/objfloat: Fix handling of negative float to power of nan.Damien George2020-09-11
| | | | | | | | Prior to this commit, pow(-2, float('nan')) would return (nan+nanj), or raise an exception on targets that don't support complex numbers. This is fixed to return simply nan, as CPython does. Signed-off-by: Damien George <damien@micropython.org>
* all: Rename "sys" module to "usys".stijn2020-09-04
| | | | | | | | | This is consistent with the other 'micro' modules and allows implementing additional features in Python via e.g. micropython-lib's sys. Note this is a breaking change (not backwards compatible) for ports which do not enable weak links, as "import sys" must now be replaced with "import usys".
* all: Update Python code to conform to latest black formatting.Damien George2020-08-29
| | | | | | | | | | | | | Updating to Black v20.8b1 there are two changes that affect the code in this repository: - If there is a trailing comma in a list (eg [], () or function call) then that list is now written out with one line per element. So remove such trailing commas where the list should stay on one line. - Spaces at the start of """ doc strings are removed. Signed-off-by: Damien George <damien@micropython.org>
* tests: Split out complex reverse-op tests to separate test file.Damien George2020-08-29
| | | | | | | So they can be skipped if __rOP__'s are not supported on the target. Also fix the typo in the complex_special_methods.py filename. Signed-off-by: Damien George <damien@micropython.org>
* py/objcomplex: Add mp_obj_get_complex_maybe for use in complex bin-op.Damien George2020-06-27
| | | | | | | | This allows complex binary operations to fail gracefully with unsupported operation rather than raising an exception, so that special methods work correctly. Signed-off-by: Damien George <damien@micropython.org>
* 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.
* py/objint: Do not use fpclassify.stijn2020-04-18
| | | | | | | | | | | | For combinations of certain versions of glibc and gcc the definition of fpclassify always takes float as argument instead of adapting itself to float/double/long double as required by the C99 standard. At the time of writing this happens for instance for glibc 2.27 with gcc 7.5.0 when compiled with -Os and glibc 3.0.7 with gcc 9.3.0. When calling fpclassify with double as argument, as in objint.c, this results in an implicit narrowing conversion which is not really correct plus results in a warning when compiled with -Wfloat-conversion. So fix this by spelling out the logic manually.
* tests/float: Fix cmath_fun_special for MICROPY_FLOAT_IMPL_FLOAT.stijn2020-04-18
| | | | | | | | | When the unix and windows ports use MICROPY_FLOAT_IMPL_FLOAT instead of MICROPY_FLOAT_IMPL_DOUBLE, the test output has for example complex(-0.15052, 0.34109) instead of the expected complex(-0.15051, 0.34109). Use one decimal place less for the output printing to fix this.
* tests/float: Add new lexer test to test parsing of float without prefix.David Lechner2020-03-30
| | | | | | Since automatically formatting tests with black, we have lost one line of code coverage. This adds an explicit test to ensure we are testing the case that is no longer covered implicitly.
* tests: Format all Python code with black, except tests in basics subdir.David Lechner2020-03-30
| | | | | | | | | | This adds the Python files in the tests/ directory to be formatted with ./tools/codeformat.py. The basics/ subdirectory is excluded for now so we aren't changing too much at once. In a few places `# fmt: off`/`# fmt: on` was used where the code had special formatting for readability or where the test was actually testing the specific formatting.
* tests/basics: Add tests for equality between bool and int/float/complex.Damien George2020-02-11
| | | | | False/True should be implicitly converted to 0/1 when compared with numeric types.
* tests: Add boolean-as-integer formatting tests for fixed regression.Yonatan Goldschmidt2020-01-24
| | | | As suggested by @dpgeorge in #5538.
* tests: Rename "array" module to "uarray".Damien George2019-10-22
|
* 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/objfloat: Fix abs(-0.0) so it returns 0.0.Damien George2018-09-27
| | | | | | Nan and inf (signed and unsigned) are also handled correctly by using signbit (they were also handled correctly with "val<0", but that didn't handle -0.0 correctly). A test case is added for this behaviour.
* 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.
* tests/float/float_parse.py: Add tests for accuracy of small decimals.Damien George2018-09-20
|
* tests/float: Test -inf and some larger values for special math funcs.Damien George2018-09-04
|
* tests/float/cmath_fun.py: Fix truncation of small real part of complex.Damien George2018-09-04
|
* py/lexer: Add support for underscores in numeric literals.Damien George2018-06-12
| | | | This is a very convenient feature introduced in Python 3.6 by PEP 515.
* tests: Add some tests for bigint hash, float hash and float parsing.Damien George2018-05-21
| | | | Following outcome of recent fuzz testing and sanitizing by @jepler.
* tests/float/float_parse: Allow test to run on 32-bit archs.Damien George2018-05-11
| | | | | | | Printing of uPy floats can differ by the floating-point precision on different architectures (eg 64-bit vs 32-bit x86), so it's not possible to using printing of floats in some parts of this test. Instead we can just check for equivalence with what is known to be the correct answer.
* py/formatfloat: Fix case where floats could render with negative digits.Damien George2018-03-01
| | | | | | Prior to this patch, some architectures (eg unix x86) could render floats with "negative" digits, like ")". For example, '%.23e' % 1e-80 would come out as "1.0000000000000000/)/(,*0e-80". This patch fixes the known cases.