circuitpython/tests/float
Yoctopuce dev dbbaa959c8 py/formatfloat: Improve accuracy of float formatting code.
Following discussions in PR #16666, this commit updates the float
formatting code to improve the `repr` reversibility, i.e. the percentage of
valid floating point numbers that do parse back to the same number when
formatted by `repr` (in CPython it's 100%).

This new code offers a choice of 3 float conversion methods, depending on
the desired tradeoff between code size and conversion precision:

- BASIC method is the smallest code footprint

- APPROX method uses an iterative method to approximate the exact
  representation, which is a bit slower but but does not have a big impact
  on code size.  It provides `repr` reversibility on >99.8% of the cases in
  double precision, and on >98.5% in single precision (except with REPR_C,
  where reversibility is 100% as the last two bits are not taken into
  account).

- EXACT method uses higher-precision floats during conversion, which
  provides perfect results but has a higher impact on code size.  It is
  faster than APPROX method, and faster than the CPython equivalent
  implementation.  It is however not available on all compilers when using
  FLOAT_IMPL_DOUBLE.

Here is the table comparing the impact of the three conversion methods on
code footprint on PYBV10 (using single-precision floats) and reversibility
rate for both single-precision and double-precision floats.  The table
includes current situation as a baseline for the comparison:

              PYBV10  REPR_C   FLOAT  DOUBLE
    current = 364688   12.9%   27.6%   37.9%
    basic   = 364812   85.6%   60.5%   85.7%
    approx  = 365080  100.0%   98.5%   99.8%
    exact   = 366408  100.0%  100.0%  100.0%

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-08-01 00:47:33 +10:00
..
array_construct.py tests: Replace umodule with module everywhere. 2023-06-08 17:54:24 +10:00
builtin_float_abs.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
builtin_float_hash.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
builtin_float_minmax.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
builtin_float_pow.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
builtin_float_round.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
builtin_float_round_intbig.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
bytearray_construct_endian.py tests: Replace umodule with module everywhere. 2023-06-08 17:54:24 +10:00
bytes_construct_endian.py tests: Replace umodule with module everywhere. 2023-06-08 17:54:24 +10:00
cmath_dunder.py tests/float: Skip new complex tests if complex unavailable. 2023-02-09 15:20:00 +11:00
cmath_fun.py py/obj: Fix REPR_C bias toward zero. 2025-07-24 11:07:30 +10:00
cmath_fun_special.py tests/float: Fix cmath_fun_special for MICROPY_FLOAT_IMPL_FLOAT. 2020-04-18 22:36:49 +10:00
complex1.py py/parsenum: Fix parsing complex literals with negative real part. 2025-06-10 15:41:24 +10:00
complex1_intbig.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
complex_dunder.py py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first. 2022-07-25 16:11:26 +10:00
complex_reverse_op.py tests: Split out complex reverse-op tests to separate test file. 2020-08-29 14:12:20 +10:00
complex_special_methods.py tests: Split out complex reverse-op tests to separate test file. 2020-08-29 14:12:20 +10:00
float1.py all: Update Python formatting to latest Black version 22.1.0. 2022-02-02 16:49:55 +11:00
float2int_doubleprec_intbig.py tests/float: Use "not" instead of ~ to invert bool value. 2024-05-28 10:49:22 +10:00
float2int_fp30_intbig.py tests/float: Use "not" instead of ~ to invert bool value. 2024-05-28 10:49:22 +10:00
float2int_intbig.py tests/float: Use "not" instead of ~ to invert bool value. 2024-05-28 10:49:22 +10:00
float_array.py py/obj: Fix nan handling in object REPR_C and REPR_D. 2025-06-24 00:30:08 +10:00
float_compare.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
float_divmod.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
float_divmod_relaxed.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
float_dunder.py py/obj: Add support for __float__ and __complex__ functions. 2022-07-25 14:23:34 +10:00
float_format.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
float_format_accuracy.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
float_format_ftoe.py py/formatfloat: Format all whole-number floats exactly. 2022-07-26 22:23:47 +10:00
float_format_ftoe.py.exp py/formatfloat: Format all whole-number floats exactly. 2022-07-26 22:23:47 +10:00
float_format_ints.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
float_format_ints_doubleprec.py py/formatfloat: Use pow(10, e) instead of pos/neg_pow lookup tables. 2022-08-12 23:53:34 +10:00
float_format_ints_power10.py qemu: Rename qemu-arm port to qemu. 2024-09-06 11:08:39 +10:00
float_parse.py py/parsenum: Reduce code footprint of mp_parse_num_float. 2025-02-28 13:35:12 +11:00
float_parse_doubleprec.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
float_struct.py py/binary: Support half-float 'e' format in struct pack/unpack. 2024-03-20 14:13:49 +11:00
float_struct_e.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
float_struct_e_doubleprec.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
float_struct_e_fp30.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
inf_nan_arith.py tests/float/inf_nan_arith.py: Include -inf in argument combos. 2023-12-06 14:06:01 +11:00
int_64_float.py py/objint_longlong: Fix longlong interoperability with floats. 2025-07-29 01:14:35 +10:00
int_big_float.py all: Update Python formatting to latest Black version 22.1.0. 2022-02-02 16:49:55 +11:00
int_divzero.py all: Update Python formatting to latest Black version 22.1.0. 2022-02-02 16:49:55 +11:00
int_power.py all: Update Python formatting to latest Black version 22.1.0. 2022-02-02 16:49:55 +11:00
lexer.py tests/float: Add new lexer test to test parsing of float without prefix. 2020-03-30 13:23:05 +11:00
list_index.py tests: Rename test scripts, changing - to _ for consistency. 2014-07-05 06:14:29 +01:00
math_constants.py tests/float/math_constants.py: Test actual e and pi constant values. 2025-05-22 17:19:03 +10:00
math_constants_extra.py py/obj: Fix nan handling in object REPR_C and REPR_D. 2025-06-24 00:30:08 +10:00
math_domain.py tests/float/math_domain.py: Tweak test to also pass with obj-repr-C. 2023-09-29 12:02:21 +10:00
math_domain_python311.py tests/float: Test domain errors for more combos of args to math funcs. 2023-06-18 12:45:55 +10:00
math_domain_python311.py.exp tests/float: Test domain errors for more combos of args to math funcs. 2023-06-18 12:45:55 +10:00
math_domain_special.py tests/float: Make output of math function tests more readable. 2023-02-16 10:38:38 +11:00
math_dunder.py py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first. 2022-07-25 16:11:26 +10:00
math_factorial_intbig.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
math_fun.py tests/float: Make output of math function tests more readable. 2023-02-16 10:38:38 +11:00
math_fun_bool.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
math_fun_int.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
math_fun_intbig.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
math_fun_special.py all: Go back to using default ruff quote style. 2025-07-24 12:48:18 +10:00
math_isclose.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
math_isclose.py.exp py/modmath: Implement math.isclose() for non-complex numbers. 2019-08-17 23:23:17 +10:00
python36.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
python36.py.exp py/lexer: Add support for underscores in numeric literals. 2018-06-12 12:17:43 +10:00
string_format.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
string_format2.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
string_format_modulo.py py/formatfloat: Use pow(10, e) instead of pos/neg_pow lookup tables. 2022-08-12 23:53:34 +10:00
string_format_modulo2.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
string_format_modulo2_intbig.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
string_format_modulo3.py py/formatfloat: Improve accuracy of float formatting code. 2025-08-01 00:47:33 +10:00
true_value.py tests: Format all Python code with black, except tests in basics subdir. 2020-03-30 13:21:58 +11:00
types.py tests: Split out those tests requiring float and import. 2014-04-17 16:21:43 +01:00