Hey Guys,
I am trying to implement the log2f() function on the C2000 but it doesn't appear to be included in the math.h library.
Does anyone know if this is available somehow with the C2000 compiler?
Thanks,
Cyril
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hey Guys,
I am trying to implement the log2f() function on the C2000 but it doesn't appear to be included in the math.h library.
Does anyone know if this is available somehow with the C2000 compiler?
Thanks,
Cyril
Hi Cyril,
There is no native log2() in the run-time support library but you can use the mathematical equivalent:
log2(x) = ln(x)/ln(2)
= 1.442695 * ln(x)
If you are trying to take the log2 of powers of two, you would probably get a decimal approx to the correct value and this creates a bit of the problem if you are trying to use the answer in a logical decision or some kind of integer math. e.g 1024
int a = ln(1024) * 1.442695
= (int)(9.9999997...)
= 9
In cases like this I would add 0.5 and then truncate or cast to int. Im not sure if this is always the right thing to do...so you might want to get a second opinion on this.
maybe i spoke too hastily.
cmath has log2 and log2f. so instead of math.h, #include <cmath>
Vishai, Thank you so much for the quick replies.
I have done the log()/log(2) trick for now and shortening it to a floating multiply approximation.
I did try including <cmath> but the compiler couldn't find it and a file search couldn't find it either.
Best,
Cyril
Hmm, odd....it should be in the ccs installation folder. The one I was referring to was under C:\TI\ccsv5\tools\compiler\c2000_6.1.0\include on my machine.
cmath is a C++11 standard header file, so you would have to include it in a .cpp file only
http://en.cppreference.com/w/cpp/header/cmath
Apparently, the C99 standard math.h does not implement log2
http://pubs.opengroup.org/onlinepubs/7908799/xsh/math.h.html
Vishai,
Thanks for this. This is declared as an inline function defined in a .h file. I included <cmath> in that file, but during compiling, it said "error: indentifier "log2" is undefined".
Perhaps what you are saying is that I can only include the <cmath> header and the log2() function from inside the specific .cpp file?
For example, in "types.h"
#include <cmath>
......
inline f32 logConversion( f32 input)
{ return log2(input/440.0); }
This function is then called in a file caled "scale.cpp"
This is what is not working....
Thanks
Cyril,
First, let me correct an earlier statement. log2 is part of the C99 standard but not C89 or C90
I spoke with our compiler folks; Our compilers provide C89 support and do not currently support log2 but v6.3 of the compiler tools, available next year, will support log2.
For the time being, you are stuck with the ln()/ln(2) equivalent.
Got it Vishai. Thank you very much for following through on this for me. It is much appreciated.
Cyril