title: C Double comparison in if statement not working as expected
body: <p>I am writing a program which takes an input from the user as a whole line, 
  then parses the integers into an array and eventually calculates the average value and the value closest to the average.</p>
  
  <p>I am running Windows 10, Dev C++ IDE and compiler TDM-GCC 4.9.2 64-bit</p>
  
  <p>I read that double comparison is an issue in C so I used the isless() function for more precision.
  Still for some reason the comparison is not performed as it should.
  This is my code so far:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include&lt;math.h&gt;
  #include&lt;float.h&gt;
  
  int main(){
  
  char string[250];
  
  //Prompt user to input sequence of integers
  printf("Please enter the integer values: ");
  
  //Read whole line from input
  fgets (string, 250, stdin);
  
  
  //Remove non-numeric characters from sequence and replace them with ' ' (space)
  int j;
  for(j=0; j&lt;strlen(string); j++)
  {
      if(!isdigit(string[j]))
      string[j] = ' ';
  }
  
  char *token;
  char *rest = string;
  int array[50]={0};
  int i=0;
  
  
  //Break string into Sub-tokens with ' ' as delimiter
  // and parse them into an int array
  while((token = strtok_r(rest, " ", &amp;rest)))
  {
      array[i]=atoi(token);
      i++;
  }
  
  
  double average,counter,sum;
  i=0;
  counter=0;
  sum=0;
  
  //Calculate the average
  while(array[i]!=0)
  {
      sum=sum+array[i];
      counter++;
      i++;
  
  }
  average=sum/counter;
  //printf("Counter %lf",counter);
  //printf("sum %lf", sum);
  printf("Average : %lf\n",average);
  
  
  int index=0;
  i=0;
  double difference;
  while(array[i]!=0)
  {
      if (i==0)
      {
          difference=fabs(fabs(array[0])-fabs(average));
      }
  
      if(isless((fabs(array[i])-fabs(average)),difference))
      {
          index=i;
      }
  
  
      printf("index %i difference %lf \n", index, fabs(fabs(array[i])-fabs(average)));
      printf("i value %i \n",i);
      i++;
  }
  
  printf("Closest element : %i\n", array[index]);
  
  
  return 0;
  
  }
  </code></pre>
  
  <p>Is there another way to compare the 2 values other than the isless function?
  A test run of the program gives the following output:
  <a href="https://i.stack.imgur.com/o6rrv.png" rel="nofollow noreferrer">Test Output</a></p>
  
  <p>Please let me know if there exists another way to do the comparison in the last if.</p>