I am having some weird floating point issues printed as NaN, depending on where in my code I print them. For context, the code sorts the list of earthquake data using BST and then iterates over the sorted data to find the largest gap between two consecutive earthquakes. These two earthquakes are then printed here:
FILE* output_file = safe_open_file("task_1_bst.csv", "w");
fprintf(output_file, "timestamp,latitude,longitude,magnitude\n");
eq_print(output_file, sorted_arr[longest_index-1]);
fprintf(output_file, "\n");
eq_print(output_file, sorted_arr[longest_index-1]);
fprintf(output_file, "\n");
eq_print(output_file, sorted_arr[longest_index]);
fclose(output_file);
As you can see, I type one of the earthquakes twice, and this is the result:
timestamp,latitude,longitude,magnitude
2009-06-13T06:02:52.500Z,nan,-115.392,3.4
2009-06-13T06:02:52.500Z,31.315,-115.392,3.4
2009-06-13T16:04:06.650Z,3.930,126.648,4.4
For some reason, the latitude of the first EQ is nan when it is first printed, but printed correctly a second time.
There is quite a lot of code, so I cannot include it here. The array sorted_arr is full of pointers to eq_t types.
typedef struct {
timestamp_t* timestamp;
float latitude;
float longitude;
float magnitude;
float x;
float y;
} eq_t;
timestamp_t - (, , , ..), eq_print - fprintf, eq_t timestamp_t.
void eq_print(FILE* fp, eq_t* q)
{
fprintf(fp, "%d-%02d-%02dT%02d:%02d:%02d.%03dZ,%.3f,%.3f,%.1f",
q->timestamp->year,
q->timestamp->month,
q->timestamp->day,
q->timestamp->hour,
q->timestamp->min,
q->timestamp->sec,
q->timestamp->msec,
q->latitude,
q->longitude,
q->magnitude);
}
eq_print eq_t, , fprintfs ?
, NaN, ?
: GDB .
Breakpoint 1, task_1_find_longest_break_after_2k_bst (
eq_csv_file=0x28cc87 "eq_data.csv") at tasks.c:128
128 FILE* output_file = safe_open_file("task_1_bst.csv", "w");
(gdb) warning: cYgFFFFFFFF 611B75D0 0
warning: cYgstd 0x28cbdf d 3
print sorted_arr[longest_index-1]->latitude
$1 = 31.3150005
(gdb) next
129 fprintf(output_file, "timestamp,latitude,longitude,magnitude\n");
(gdb) print sorted_arr[longest_index-1]->latitude
$2 = 31.3150005
(gdb) next
130 eq_print(output_file, sorted_arr[longest_index-1]);
(gdb) print sorted_arr[longest_index-1]->latitude
$3 = 31.3150005
(gdb) next
131 fprintf(output_file, "\n");
(gdb) print sorted_arr[longest_index-1]->latitude
$4 = 31.3150005
(gdb) next
132 eq_print(output_file, sorted_arr[longest_index-1]);
(gdb) print sorted_arr[longest_index-1]->latitude
$5 = 31.3150005
, , .
Eq_t q
eq_t* read_quake(FILE* fp)
{
char buf[1024];
float latitude, longitude, magnitude;
if (fscanf(fp, "%[^,],%f,%f,%f\n", buf, &latitude, &longitude, &magnitude) == 4) {
eq_t* eq = (eq_t*)safe_malloc(sizeof(eq_t));
eq->timestamp = parse_time(buf);
eq->latitude = latitude;
eq->longitude = longitude;
eq->magnitude = magnitude;
map_coordinates(eq);
return eq;
}
return NULL;
}
. .