コード例 #1
0
ファイル: utils.c プロジェクト: yfu/datamash
long double
stdev_value (const long double * const values, size_t n, int df)
{
  return sqrtl ( variance_value ( values, n, df ) );
}
コード例 #2
0
ファイル: field-ops.c プロジェクト: charles-plessy/compute
/* Prints to stdout the result of the field operation,
   based on collected values */
void
field_op_summarize (struct fieldop *op)
{
  bool print_numeric_result = true;
  long double numeric_result = 0 ;
  char *string_result = NULL;

#ifdef ENABLE_DEBUG
  if (debug)
    fprintf (stderr, "-- summarize for %s(%zu)\n", op->name, op->field);
#endif

  switch (op->op)
    {
    case OP_MEAN:
      numeric_result = op->value / op->count;
      break;

    case OP_SUM:
    case OP_COUNT:
    case OP_MIN:
    case OP_MAX:
    case OP_ABSMIN:
    case OP_ABSMAX:
      /* no summarization for these operations, just print the value */
      numeric_result = op->value;
      break;

    case OP_MEDIAN:
      field_op_sort_values (op);
      numeric_result = median_value ( op->values, op->num_values );
      break;

    case OP_PSTDEV:
      numeric_result = stdev_value ( op->values, op->num_values, DF_POPULATION);
      break;

    case OP_SSTDEV:
      numeric_result = stdev_value ( op->values, op->num_values, DF_SAMPLE);
      break;

    case OP_PVARIANCE:
      numeric_result = variance_value ( op->values, op->num_values,
                                        DF_POPULATION);
      break;

    case OP_SVARIANCE:
      numeric_result = variance_value ( op->values, op->num_values,
                                        DF_SAMPLE);
      break;

    case OP_MODE:
    case OP_ANTIMODE:
      field_op_sort_values (op);
      numeric_result = mode_value ( op->values, op->num_values,
                                    (op->op==OP_MODE)?MODE:ANTIMODE);
      break;

    case OP_UNIQUE:
      print_numeric_result = false;
      string_result = unique_value (op, case_sensitive);
      break;

    case OP_COLLAPSE:
      print_numeric_result = false;
      string_result = collapse_value (op);
      break;

   case OP_COUNT_UNIQUE:
      numeric_result = count_unique_values(op,case_sensitive);
      break;

    default:
      /* Should never happen */
      error (EXIT_FAILURE, 0, _("internal error 2"));
    }


#ifdef ENABLE_DEBUG
  if (debug)
    {
      if (print_numeric_result)
        fprintf (stderr, "%s(%zu) = %Lg\n", op->name, op->field, numeric_result);
      else
        fprintf (stderr, "%s(%zu) = '%s'\n", op->name, op->field, string_result);
    }
#endif

  if (print_numeric_result)
    printf ("%Lg", numeric_result);
  else
    printf ("%s", string_result);

  free (string_result);
}