Skip to content
Advertisement

Why does Micropython ‘const’ not accept float values?

Most Micropython ports contain a ‘micropython‘ module, which has a specific function called ‘const()‘. I am led to believe it behaves a lot like ‘#define‘ in C, however, it only accepts integers. Does anyone know why this is? You can declare float constants in C, so why is this unavailable in micropython?

I want to efficiently store values which are used to convert between units, but many of them are float values… (I thought about multiplying them by 10^x and then just dividing by that at runtime, but this will likely take just as long as storing the float in a variable and letting the code use the lookup table).

Any ideas why we can only declare integers with micropython.const()?

Cheers :)

Advertisement

Answer

With the recent release of v1.19, you can apply const to any object. See PR #8548 for some of the details.

In short, the reason const was initially constrained to ints was simply because the implementation to do so was relatively straightforward.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement