Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter accept wrong type and still runs fine — why?

Below I have a method I search from the internet to calculate the percentrank function of Excel in C#. I modify a bit to suit my program but didn't change the main logic.

The program compiles and runs fine without any error (that I am aware of). However further checking my code, in my main, I call the function using

        double result = percentRank( array, x); 

where

x is an int
array is a List (int)

It is of a different type than what percentRank method is specified to take, but it still runs 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 the answer is implicit type conversion. Can I disable it? I don't like it because it may generate some bugs that I am not aware of.

like image 205
Clayton Leung Avatar asked Dec 08 '22 20:12

Clayton Leung


1 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 this here:

double value = 3;

This is allowed because of the same implicit conversion. Without that conversion, you would have to type:

double value = 3.0;

This is specified in the C# Language Specification, section "6.1.2 Implicit numeric conversions"

The implicit numeric conversions are:

...

  • From int to long, float, double, or decimal.
like image 59
Reed Copsey Avatar answered Dec 11 '22 11:12

Reed Copsey