2 Calculate the maximum relative energy error in a trajectory.
4 Copyright (C) 2013 W. Trevor King
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h> /* for *printf(), getc(), fscanf(), EOF, stdin, stderr */
21 #include <math.h> /* for fabs() */
23 /* Read a double column from the current line in a tab-delimited
26 int read_column(FILE *stream, int column, size_t line, double *value)
30 while ((c = getc(stream)) != EOF) {
31 if (c == '\t') { /* found a new column */
33 if (col == column) { /* read our value */
34 c = fscanf(stream, "%lf", value);
36 fprintf(stderr, "could not parse column %d in line %ld\n",
40 while ((c = getc(stream)) != EOF)
42 return 0; /* advanced to next line */
44 } else if (c == '\n') {
45 fprintf(stderr, "only %d columns in line %ld\n", column, line);
49 return -1; /* reached the end of the file */
52 int main(int argc, char **argv) {
54 double val_i, val, error, max_error = 0;
56 for (line=0; read_column(stdin, column, line, &val) == 0; line++) {
61 "cannot calculate relative error when initial value is zero\n");
65 error = fabs((val - val_i)/(val_i));
66 if (error > max_error)
70 printf("%g\n", max_error);