Problem with log2 and pow functions in mbed

06 Jun 2017

I uploaded a program on the online compiler mbed that uses the functions log2 and pow of the library math.h and gives me the following error: Identifier "log2" is undefined "num_bit=1+log2(decimale);" Expression must have integral or enum type "temp[num_bit-1-i]=decimale%2+'0';" More than once instance of overloaded function "pow" matches the argument list "dec=dec+(binario[j]-'0' *pow(2, 5-j);" Where is the problem?

08 Jun 2017

Hello Simone,

  • There is no log2 function defined in the math library linked by the online compiler, so you have to define one by yourself.
  • The pow function is overloaded, i.e. it is defined with various argument lists. As a consequence, it must be called with unambiguous arguments (signature) - see https://developer.mbed.org/questions/77726/mathh-function-pow-error for more explanation.

For example the following code doesn't do anything useful but compiles with no errors:

#include "mbed.h"
#include "math.h"

#define _M_LN2  0.693147180559945309417 // Natural log of 2
#define log2(x) (log(x)/_M_LN2)

int main(void) {
    
    float   decimale = 12;
    int     dec;
    int     temp[100];
    int     binario[100];
    uint8_t i = 0;
    uint8_t j = 0;

    int num_bit = 1 + log2(decimale);
    temp[num_bit - 1 - i] = (int)decimale % 2 + '0';
    dec = dec + (binario[j] - '0') * pow((float)2, (float)(5-j));

    while(1) {}
}