#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    14
    Rep Power
    0

    Error Result : Log Function in C


    Hi,

    I am getting incorrect result. Suppose i need to get R=[-1.3863, 1.3863,-1.3863,1.3863,-1.3863,-1.3863]. But its displaying some garbage values

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int i;
        double  a;
    	int  x[]={0,0,1,0,1,1};
    	int  y[]={1,0,1,0,1,1};
    	float R[6];
    	printf("Encoded Codeword \n");
    	for (i = 0; i < 6; i++)
        {
          printf("%d",x[i]);
        }
        printf("\n");
    
        printf("Enter Crossover probability: ");
       scanf("%f", &a);
    
      for (i = 0; i < 6; i++)
        {
            if (y[i]==1)
            {
                R[i]=log(a/(1-a));
            }
            else
            {
              R[i]=log((1-a)/a);
            }
            printf ("%.2f \n",R[i]);
        }
    	return 0;
    }
    i have given Crossover probability as 0.2.
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,380
    Rep Power
    9645
    float and double are not the same thing. Pick one type and stick with it.

    - a is double
    - R is float[]
    - %f is for float
    - log() returns double

    If you want float then use float everywhere and logf() instead of log().

    Comments on this post

    • Saras agrees

IMN logo majestic logo threadwatch logo seochat tools logo