Add a new MICROPY_COMP_CONST_FLOAT feature, enabled by in mpy-cross and
when compiling with MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES. The new
feature leverages the code of MICROPY_COMP_CONST_FOLDING to support folding
of floating point constants.
If MICROPY_COMP_MODULE_CONST is defined as well, math module constants are
made available at compile time. For example:
_DEG_TO_GRADIANT = const(math.pi / 180)
_INVALID_VALUE = const(math.nan)
A few corner cases had to be handled:
- The float const folding code should not fold expressions resulting into
complex results, as the mpy parser for complex immediates has
limitations.
- The constant generation code must distinguish between -0.0 and 0.0, which
are different even if C consider them as ==.
This change removes previous limitations on the use of `const()`
expressions that would result in floating point number, so the test cases
of micropython/const_error have to be updated.
Additional test cases have been added to cover the new repr() code (from a
previous commit). A few other simple test cases have been added to handle
the use of floats in `const()` expressions, but the float folding code
itself is also tested when running general float test cases, as float
expressions often get resolved at compile-time (with this change).
Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
23 lines
540 B
Python
23 lines
540 B
Python
# test constant optimisation, with consts that are floats
|
|
|
|
from micropython import const
|
|
|
|
# check we can make consts from floats
|
|
F1 = const(2.5)
|
|
F2 = const(-0.3)
|
|
print(type(F1), F1)
|
|
print(type(F2), F2)
|
|
|
|
# check arithmetic with floats
|
|
F3 = const(F1 + F2)
|
|
F4 = const(F1**2)
|
|
print(F3, F4)
|
|
|
|
# check int operations with float results
|
|
F5 = const(1 / 2)
|
|
F6 = const(2**-2)
|
|
print(F5, F6)
|
|
|
|
# note: we also test float expression folding when
|
|
# we're compiling test cases in tests/float, as
|
|
# many expressions are resolved at compile time.
|