Using "powf()" with nRF52840 in Zephyr environment

I'm attempting to use the "powf()" function in a Zephyr thread.

When I try to run:

LOG_INF("powf: %f",powf(2.0f,-8.0f));
the result is "inf".     (Code compiles, but doesn't give the correct result.)
I'm using nRF Connect 2.0.2 SDK.   I set the FPU check box in the Kconfig editor.
Is there some other setting that needs to be invoked (for example in prj.conf)?
Thanks!
  • I was able to make the powf function work with NCS v2.0.2 and the nRF52840 DK by adding these Kconfigs to the prj.conf file:

    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    

    and using this code:

    #include <zephyr/zephyr.h>
    #include <math.h>
    #include <stdio.h>
    
    void main(void)
    {
    	printk("powf sample! \n");
    	double n1 = 5;
        double n2 = 2;
    	double res;
    	res = pow(n1, n2);
    	printf("pow(5, 2): %.01f\n", res);
    	
    	float n3 = 5.0;
        float n4 = 2.0;
    	float res2;
    	res2 = powf(n3, n4);
    	printf("powf(5.2): %.01f\n", res2);
    
    }
    

    Test it out yourself:

    powf_function.zip

    Best regards,

    Simon

  • Hi Simon,

    Thanks.

    I think the problem I was having is that I was trying to report a float through a LOG_INF call.    It appears that different CONFIG_ statements are needed allow float values  there.

    I've attached a new .zip that shows the needed CONFIG statements for the LOG_INF (and printk).

    On this (and other issues), is there documentation shows what "CONFIG" statements are needed for each function (almost like the old UNIX man page, where a function is described, and then specifies what CONFIG statement(s) need to be in the prf.conf file for it to work?).    I've spent way too many hours reading through code, trying to figure out why something won't compile (or run properly) and trying to figure out what CONFIG statements are needed...

    Thanks!powf_function_new.zip

  • PS...

    Yet putting this code in my application doesn't seem to work:

    yields:

    The prj.conf file has all of the same CONFIG statements...

    Not sure yet what is happening here...

  • CktDesigner said:
    Yet putting this code in my application doesn't seem to work:

    I just tested the same code snippet on my end, and it worked fine.

    Process: JLink.exe
    *** Booting Zephyr OS build v3.0.99-ncs1-1  ***
    !!!!!powf sample!!!!!!!!!!!!!!!!!!!!!!!!! 
    [00:00:00.370,880] <inf> Main: pow: 0.0312 0.0312
    pow: 0.0312 0.0312
    pow: 0.0312 0.0312

    Here is the sample (tested with NCS v2.0.2 and nrf52840dk_nrf52840):

    0027.powf_function_v3.zip

    Best regards,

    Simon

  • Hi Simon,

    Thanks...

    OK, I found the issue...   I was missing the "#include <math.h>" statement in the file that called pow()/powf()

    Dumb mistake...     But why did it compile/run without it???    It appears that not having the .h file still compiles/runs, but gives the wrong result.

    By the way, in the process of debugging, I thought I'd try upgrading from version v2.0.2 to v2.1.0...    But evidently the device-tree definitions for all of the nrf52840 peripherals changed!   Since I have a custom board, I'll have to re-write the .dts file before I can upgrade...

    Thanks...

Related