I have this function:
float calc_nnc(struct ImageWindow *window1, struct ImageWindow *window2) {
/* More code */
double numerator = (double) sum_a_x_b;
double divisor = ( sqrt(sum_a) * sqrt(sum_b) );
double result = numerator / divisor;
float resultf = (float) result;
printf("numerator: %lf, divisor: %lf, result: %lf, resultf: %f\n",
numerator, divisor, result, resultf);
return resultf;
}
The printf prints the results I expect to see:
a x b = 1383, a = 1776, b = 4959
numerator: 1383.000000, divisor: 2967.690011, result: 0.466019, resultf: 0.466019
However, when I try to print the result of calc_nnc in another function:
float nnc_likeness;
unsigned int x, y;
for (y = 0; y <= y_max; y++) {
for (x = 0; x <= x_max; x++) {
set_image_window(&window_big, big, x, y, width, height);
nnc_likeness = calc_nnc(&window_small, &window_big);
printf("likeness: %f\n", nnc_likeness);
}
}
I get garbage:
likeness: 1055824384.000000
That is, I see the correct values calc_nnc is computing, but right after that I see a corrupted value for nnc_likeness.
Any idea what’s happening? Thanks!
You probably have no prototype for
calc_nncin the context where you call it, so your compiler thinks its return type isint(as per the spec). A quick test program:Gives this output:
Include a correct prototype that tells your code that
calc_nncreturnsfloatand you’ll be all set. Turning more warnings on will catch this problem, too.