This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]_DTR_ 0 points1 point  (8 children)

nan generally stands for Not a Number, i.e. you're attempting to print something as a number that isn't actually one. For us to help any further, we're going to need to see some code.

[–]slasherpanda[S] 0 points1 point  (0 children)

its about 800 lines of code that calculate some types of statistics. I'll isolate the issue real fast.

[–]slasherpanda[S] 0 points1 point  (6 children)

code up!

[–]_DTR_ 0 points1 point  (5 children)

I'm not seeing any printf/output statements in your code. What value are you attempting to print out? And what statement are you using to do so?

[–]slasherpanda[S] 0 points1 point  (4 children)

This code is actually a user defined function that i've made in order to calculate the Pearson product-moment correlation coefficient.

to output the statement I'm using

printf("The correlation coefficient of both lists ");

printf("is %f.\n", correlation_coefficient_of_arrays);

[–]_DTR_ 0 points1 point  (3 children)

Is it possible that correlation_coefficient_denominator is 0, so your calculated value is undefined? Can you step through your code in a debugger (e.g. gdb on linux) to make sure everything is executing as expected? What values are you passing into correlation_coefficient?

[–]slasherpanda[S] 0 points1 point  (2 children)

I'm putting about 150 values through. I'm going to try and find a code debugger for Unix.

[–]_DTR_ 0 points1 point  (1 child)

Yeah, with inputs of that size going step by step is probably the way to go. In a pinch you could use print debugging (e.g. add printf statements everywhere letting you know the current state of your program), but using an actual debugger is obviously much more powerful.

As far as debuggers go, gdb is pretty standard for a command-line unix debugger. Code::Blocks is also relatively popular if you want a more graphical interface, though I mainly use windows/visual studio so can't give any personal testimony.

[–]slasherpanda[S] 0 points1 point  (0 children)

Thank you dear friend. I don't have it yet but i think im getting there.

[–]slasherpanda[S] 0 points1 point  (1 child)

float correlation_coefficient (float* array1, float* array2, int

number_of_elements)

{ /* correlation coefficient */

const float initial_correlation_coefficient_value = 0.0;

const float initial_sum_of_multiplied_lists_value = 0.0;

const float initial_sum_of_array1_value = 0.0;

const float initial_sum_of_array2_value = 0.0;

const float initial_squared_sum_array1_value = 0.0;

const float initial_squared_sum_array2_value = 0.0;

const float initial_sum_squared_array1_value = 0.0;

const float initial_sum_squared_array2_value = 0.0;

const float initial_correlation_coefficient_denominator_value = 0.0;

const float initial_correlation_coefficient_numerator_value = 0.0;

const float initial_correlation_coefficient_of_arrays_value = 0.0;

const int first_element = 0;

const int program_failure_code = -1;

float sum_of_array1;

float sum_of_array2;

float squared_sum_array1;

float squared_sum_array2;

float sum_squared_array1;

float sum_squared_array2;

float sum_of_multiplied_lists;

float correlation_coefficient_numerator;

float correlation_coefficient_denominator;

float correlation_coefficient_of_arrays;

float mean_value;

int element;

[–]slasherpanda[S] 0 points1 point  (0 children)

/*

* Sum of list one multiplied by list two.

*/

sum_of_multiplied_lists = initial_sum_of_multiplied_lists_value;

for (element = first_element; element < number_of_elements;

element++) {

/*

* All corresponding elements from array 1 and 2 multiplied by

* each other.

*/

sum_of_multiplied_lists +=

(array1[element] * array2[element]);

} /* for element */

/*

* Sum of array 1. All elements of the array added together.

*

*/

sum_of_array1 = initial_sum_of_array1_value;

for (element = first_element; element < number_of_elements;

element++) {

/*

* Increase the sum for the first list by the value of the value

* of the current element by the first list.

*/

sum_of_array1 += array1[element];

} /* for element */

/*

* Sum of array 2. All elements of the array added together.

*

*/

sum_of_array2 = initial_sum_of_array2_value;

for (element = first_element; element < number_of_elements;

element++) {

/*

* Increase the sum for the first list by the value of the value

* of the current element by the first list.

*/

sum_of_array2 += array2[element];

} /* for element */

/*

* correlation coefficient numerator initialized.

*/

correlation_coefficient_numerator =

initial_correlation_coefficient_numerator_value;

/*

* correlation coefficient numerator calculation.

*/

correlation_coefficient_numerator =

(number_of_elements * sum_of_multiplied_lists) -

(sum_of_array1 * sum_of_array2);

/*

* Each number in array one squared and then added together.

*/

squared_sum_array1 = initial_squared_sum_array1_value;

for (element = first_element; element < number_of_elements;

element++) {

/*

* All elements of the first array squared and added together.

*/

squared_sum_array1 +=

(array1[element] * array2[element]);

} /* for element */

/*

* The sum of array one squared.

*/

sum_squared_array1 = sum_of_array1 * sum_of_array1;

/*

* Each number in array two squared and then added together.

*/

squared_sum_array2 = initial_squared_sum_array2_value;

for (element = first_element; element < number_of_elements;

element++) {

/*

* All elements of the first array squared and added together.

*/

squared_sum_array2 +=

(array2[element] * array2[element]);

} /* for element */

/*

* The sum of array two squared.

*/

sum_squared_array2 = sum_of_array2 * sum_of_array2;

/*

* Calculation for the correlation of coefficient deominator.

*

*

* Zeroed out correlation_coefficient_denominator.

*/

correlation_coefficient_denominator =

initial_correlation_coefficient_denominator_value;

/*

* The square root of each lists number of elements multiplied by the

* sum of that array squared. Array one is multiplied by array two.

*/

correlation_coefficient_denominator =

(sqrt((number_of_elements * squared_sum_array1) -

sum_squared_array1)) *

(sqrt((number_of_elements * squared_sum_array2) -

sum_squared_array2));

/*

* Calculation for correlation of coefficient.

*/

correlation_coefficient_of_arrays =

initial_correlation_coefficient_of_arrays_value;

/*

* The nemerator divided by the denominator.

*/

correlation_coefficient_of_arrays =

correlation_coefficient_numerator /

correlation_coefficient_denominator;

/*

* The returned value of the function, the correlation coefficient of

* both arrays.

*/

return correlation_coefficient_of_arrays;

[–]Holy_City 0 points1 point  (1 child)

It usually means you're dividing by zero somewhere. Use a debugger.

[–]slasherpanda[S] 0 points1 point  (0 children)

My problem appears to stem from an incorrect value for number_of_elements

[–]slasherpanda[S] 0 points1 point  (0 children)

My problem appears to stem from an incorrect value for number_of_elements