How to use mathematical functions

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ales.coppelli
Posts: 32
Joined: Wed Aug 08, 2018 9:15 am

How to use mathematical functions

Post by ales.coppelli » Mon Feb 01, 2021 11:17 am

Hi to all.

I'm developing a simple lsm303dhlc accelerometer driver
like user module so I'm writing the C version that
which will be added and exposed to the Python interpreter.
( I have a STM32F411 Discovery board and the lsm303dhlc
accelerometer is hard linked to the PB6(scl) and PB9(sda) board pin).

In the user module directory there is this 'micropython.mk' file:

Code: Select all

USERMODULES_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(USERMODULES_DIR)/lsm303dlhc.c

CFLAGS_USERMOD += -I$(USERMODULES_DIR)

All ok until now.

I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )

I thought it was enough to add

Code: Select all

#include <math.h>
in "lsm303dlhc.c" and add the line

Code: Select all

LDFLAGS_USERMOD +=  -lm 
in 'micropython.mk'.

But that's not enough, it doesn't work.
What should I do?

stijn
Posts: 573
Joined: Thu Apr 24, 2014 9:13 am

Re: How to use mathematical functions

Post by stijn » Mon Feb 01, 2021 11:54 am

it doesn't work.
Please post the actual error you're getting otherwise it's really hard to produce sensible answers.

ales.coppelli
Posts: 32
Joined: Wed Aug 08, 2018 9:15 am

Re: How to use mathematical functions

Post by ales.coppelli » Mon Feb 01, 2021 12:21 pm

Sorry for my incomplete post..
Here the logs:

Code: Select all

CC ../../../project/lsm303dlhc/lsm303dlhc.c
CC ../../lib/oofatfs/ff.c
CC ../../lib/oofatfs/ffunicode.c
CC build-STM32F411DISC/pins_STM32F411DISC.c
LINK build-STM32F411DISC/firmware.elf
arm-none-eabi-ld: cannot find -lm
Makefile:618: recipe for target 'build-STM32F411DISC/firmware.elf' failed
make: *** [build-STM32F411DISC/firmware.elf] Error 1

User avatar
jimmo
Posts: 2244
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to use mathematical functions

Post by jimmo » Tue Feb 02, 2021 4:09 am

ales.coppelli wrote:
Mon Feb 01, 2021 11:17 am
I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )
Likely you're compiling for single-precision float, in which case you should be using sqrtf not sqrt. sqrt will only be available when compiling for double-precision. Both are provided by MicroPython's math library (lib/libm and lib/libm_double).

In general though, use MICROPY_FLOAT_C_FUN which will use the correct version automatically based on single/double precision.

e.g. MICROPY_FLOAT_C_FUN(sqrt)(variance)
ales.coppelli wrote:
Mon Feb 01, 2021 11:17 am
LDFLAGS_USERMOD += -lm
-lm would be what you'd use if you were compiling for a non-baremetal target. We use the copy in lib/libm instead (and the Makefile already handles this).

ales.coppelli
Posts: 32
Joined: Wed Aug 08, 2018 9:15 am

Re: How to use mathematical functions

Post by ales.coppelli » Wed Feb 03, 2021 6:59 am

Thank you very much: always precise and decisive.

v923z
Posts: 156
Joined: Mon Dec 28, 2015 6:19 pm

Re: How to use mathematical functions

Post by v923z » Sun Feb 07, 2021 4:51 pm

ales.coppelli wrote:
Mon Feb 01, 2021 11:17 am

I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )
Pretty much everything you can think of is already implemented in ulab: https://github.com/v923z/micropython-ulab/.

You can also directly pipe data into arrays: https://micropython-ulab.readthedocs.io ... array.html, and then operate on them in numpy fashion.

ales.coppelli
Posts: 32
Joined: Wed Aug 08, 2018 9:15 am

Re: How to use mathematical functions

Post by ales.coppelli » Mon Feb 08, 2021 4:07 pm

This library can only be compiled for some stm32 cards.
I am wrong?

P.S.= Your library is amazing!!!

v923z
Posts: 156
Joined: Mon Dec 28, 2015 6:19 pm

Re: How to use mathematical functions

Post by v923z » Mon Feb 08, 2021 5:41 pm

ales.coppelli wrote:
Mon Feb 08, 2021 4:07 pm
This library can only be compiled for some stm32 cards.
I think this is not true. Here are a couple of counter-examples:

https://github.com/v923z/micropython-ulab/#firmware

https://gitlab.com/rcolistete/micropyth ... 6/Firmware

https://gitlab.com/rcolistete/micropyth ... 2/Firmware

(Note that the compiled firmware in the links above contains a relatively old version of ulab. We have improved a lot of things since, so you might be better off compiling yourself.)

I know that circuitpython ships with ulab on nordic chips.

The library is a simple user module, therefore, I don't see, why you couldn't compile it for anything, really. If you run into difficulties, you should raise an issue on github, so that we can fix it.

Post Reply