[SOLVED]must MICROPY_FLOAT_IMPL be enabled if float-math is not performed?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 628
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]must MICROPY_FLOAT_IMPL be enabled if float-math is not performed?

Post by jickster » Fri Jun 15, 2018 5:32 pm

Enabling floats in firmware greatly increases the code size.

The user may input a float number but I will not need to perform any math on it; do I still need to enable float via
#define MICROPY_FLOAT_IMPL
I could require user surround float with strings but that seems kludgy.

Damien
Site Admin
Posts: 605
Joined: Mon Dec 09, 2013 5:02 pm

Re: must MICROPY_FLOAT_IMPL be enabled if float-math is not performed?

Post by Damien » Fri Jun 15, 2018 10:07 pm

Enabling floats is pretty much all or nothing. If you let users enter floats then they can do maths on them, e.g. 1.2+3.4. And if you accept floats in some places in your modules / C functions then the compiler must require some math support code.

You can disable the math module to save space, via MICROPY_PY_MATH.

What exactly do you need the floats for?

jickster
Posts: 628
Joined: Thu Sep 07, 2017 8:57 pm

must MICROPY_FLOAT_IMPL be enabled if float-math is not performed?

Post by jickster » Fri Jun 15, 2018 11:51 pm

Damien wrote:Enabling floats is pretty much all or nothing. If you let users enter floats then they can do maths on them, e.g. 1.2+3.4. And if you accept floats in some places in your modules / C functions then the compiler must require some math support code.

You can disable the math module to save space, via MICROPY_PY_MATH.

What exactly do you need the floats for?
Admittedly I’m not sure what the best way to implement my thing.

There’s a config() function and for one arg, the user has to pick a number from a finite list i.e. [0.5, 1.0, 2, 2.5, 5].

I can’t make it enum like [0_5, 1_0, 2_0, 2_5, 5_0] because this list could change in future versions of the firmware (and on different hw versions) so the script would break.

I could force the user to put quotes around the float - config(“0.5”) - but that seems unnatural.

Is it illogical to allow floats AND disallow float math?



Damien
Site Admin
Posts: 605
Joined: Mon Dec 09, 2013 5:02 pm

Re: must MICROPY_FLOAT_IMPL be enabled if float-math is not performed?

Post by Damien » Sat Jun 16, 2018 1:08 am

If you want to allow users to enter float objects, like in config(1.5), then that already brings a lot of "baggage" with it: the C code needs to be able to parse such numbers which means doing arithmetic behind the scenes, like 1 + 5 / 10. Then allowing users to do float arithmetic at the Python level will only cost a tiny bit more in terms of code space usage.

If you really can't afford to have floats due to size constraints then passing along strings (eg "1.5") seems like a good option. Or allow them to provide an integer that is 10 (or 1000) times the actual value, like 15 or 1500.

Post Reply