Below I have a method that I am looking from the Internet to calculate the Excel percentage function in C #. I am modifying a bit according to my program, but have not changed the main logic.
The program compiles and works without errors (what I know). However, if I continue my code, I basically call the function using
double result = percentRank( array, x);
Where
x is an int array - this is List (int)
This is a different type than the one specified for the percentRank method, but it still works fine. My question is: WHY?
private static double percentRank(List<int> array, double x) { // Calculate the PERCENTRANK(array, x) //If X matches one of the values in the array, this function is //equivalent to the Excel formula =(RANK(x)-1)/(N-1) where N is the number of data points. //If X does not match one of the values, then the PERCENTRANK function interpolates. // http://www.pcreview.co.uk/forums/algorithm-computing-excel-percentrank-t946312.html array.Sort(); double result = 0; bool foundX = false; for (int index = 0; index < array.Count; index++) { if (array[index] == x) { result = ((double)index) / ((double)array.Count - 1); foundX = true; break; } } // calculate value using linear interpolation if (foundX == false) { double x1, x2, y1, y2; x1 = x2 = x; for (int i = 0; i < array.Count - 1; i++) { if (array[i] < x && x < array[i + 1]) { x1 = array[i]; x2 = array[i + 1]; foundX = true; break; } } if (foundX == true) { y1 = percentRank(array, x1); y2 = percentRank(array, x2); result = (((x2 - x) * y1 + (x - x1) * y2)) / (x2 - x1); } else { // use the smallest or largest value in the set which ever is closer to valueX if (array[0] > x) { result = 0; } else { result = 1; } } } return result; }
EDIT: OK response is an implicit type conversion. Can I turn it off? I donβt like this because it can generate some errors that I donβt know about.
source share