How to make math logarithm functions in C99?

I’m finally back at building my functions, which is what I’m doing before making the actual BPML language. In Part 3 – Math, I want to make some logarithm functions.

I never knew what logarithm functions were at the very beginning, but as I went deeper, I learned it and made this:

float log_num(int num) {
    int mult;
    float result = 0;
    for (int i = 0; ; i++) {
        mult = 10 ^ i;
        if (mult >= num) {
            result = i;
            break;
        }
    }
    return result;
}

log_num only supports int and float and double will have their separate ones.

Now I got 2 problems with this function:

  1. When I tried to run it and use 100 as the number in the function, the result should’ve been 2.00, but it gave me 96.00.
  2. Since the value to be returned is a float, I want the function to actually give me different values if it is not a power of 10. An example of it is 2 = 0.30102999566398119521373889472449.

Q: How do I fix problem 1 and how do I make the function work as how I explained in problem 2?

I want to make the function from scratch and not relying on other functions. Yes, maybe I can use some functions I guess…

%d bloggers like this: