Logarithmic calculations without math.h

I am trying to calculate ln (x) by Taylor series. Here is my code:

#define N 10

float ln(float x){ 
    int i;
    float result;
    float xt; 
    float xtpow;
    int sign;
    if(x > 0 && x <= 1){ 
        xt = x - 1.0;
        sign = -1; 
        xtpow = 1.0;
        result = 0;
        for(i = 1 ; i < N + 1; i++ );
        {   
            // Problem here
            printf("%d\n", i); 
            sign = sign * (-1);
            xtpow *= xt; 
            result += xtpow * sign / i;
        }   
    }else if(x >= 1)
    {   
        return -1 * ln(1.0 / x); 
    }   
    return result; 
}

The problem is with my looping cycle (see above). It seems that after 1 cyclic variable ibecomes equal N + 1, nothing happens after it. Do you have any idea why this is so?

+4
source share
1 answer

It seems that after the 1loop variable ibecomes equalN + 1

delete ;after loop:

for(i = 1 ; i < N + 1; i++ );
                            ^

, { } for, i . ( " " ) get i = N + 1 .

, if(). :

 if(x > 0 && x <= 1){ <-- "True for x == 1"
   // loop code
 }
 else if(x >= 1){ <-- "True for x == 1"  
      // expression code here 
 }

, x == 1 else . .

+5

All Articles