The parameter accepts the wrong type and still works fine - why?

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.

+4
source share
4 answers

My question is: WHY?

You can assign an integer to a double value. C # will implicitly convert from Int32 to Double .

You can see it here:

 double value = 3; 

This is allowed due to the same implicit conversion. Without this conversion, you will need to enter:

 double value = 3.0; 

This is indicated in the C # Language Specification, section "6.1.2 Implicit Numeric Conversions"

Implicit numeric conversions:

...

  • From int to long, float, double or decimal.
+11
source

The C # compiler performs an implicit casting operation. A double value can contain any integer value.

+2
source

There is an implicit conversion from int to double .

The conversion is implicit, because double can hold an int value without loss of precision.

There is an explicit conversion from double to int, but without implicit . The reason is that if you store a double in an int, it will lose value when it cuts off decimal places.

MSDN has a good conversion record: http://msdn.microsoft.com/en-us/library/ms173105.aspx

+1
source

An int can be implicitly cast to double . What's going on here.

0
source

Source: https://habr.com/ru/post/1436671/


All Articles