【问题标题】:Parameter accept wrong type and still runs fine — why?参数接受错误的类型并且仍然运行良好——为什么?
【发布时间】:2012-09-19 13:55:44
【问题描述】:

下面我有一个我从网上搜索的方法来计算C#中Excel的percentrank函数。我做了一些修改以适应我的程序,但没有改变主要逻辑。

程序编译并运行良好,没有任何错误(我知道)。然而,进一步检查我的代码,在我的主要中,我使用

调用该函数
        double result = percentRank( array, x); 

在哪里

x 是一个整数
数组是一个 List (int)

它的类型与指定的 percentRank 方法不同,但它仍然运行良好。我的问题是为什么?

        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;
        }

编辑:好的,答案是隐式类型转换。我可以禁用它吗?我不喜欢它,因为它可能会产生一些我不知道的错误。

【问题讨论】:

  • 我不太明白这个问题。尽管您的变量称为“数组”,但它实际上是一个列表,因此该函数开始称为正确的类型。这看起来非常好,但我强烈建议您将参数名称更改为更好的名称(例如 cellValueList 以便您知道数据的来源)。
  • @BenjaminDangerJohnson 他在问为什么他可以将Int32 传递给定义为采用Double 的参数(“x”,而不是“array”)
  • 你不去重构这个方法吗?
  • 那很好。我以为他把他的 list 混淆为 int[] 类型

标签: c# methods parameters


【解决方案1】:

我的问题是为什么?

您可以将整数分配给双精度值。 C# 会从Int32 隐式转换为Double

你可以在这里看到:

double value = 3;

这是允许的,因为相同的隐式转换。如果没有这种转换,您将不得不输入:

double value = 3.0;

这在 C# 语言规范的“6.1.2 隐式数字转换”部分中指定

隐式数值转换为:

...

  • 从 int 到 long、float、double 或 decimal。

【讨论】:

  • 谢谢你,愚蠢的问题 --> 我可以禁用这个隐式转换吗?因为它可能会在我的程序中引入我不知道的错误?
  • @ClaytonLeung 不,这是语言本身的一部分。 Int32 总是隐式转换为 Double 如果你将它传递给一个。
【解决方案2】:

C# 编译器正在执行隐式转换操作。 double 可以保存任何整数值。

【讨论】:

    【解决方案3】:

    有一个从intdoubleimplicit 转换。

    转换是隐式的,因为 double 可以保存 int 的值而不会丢失准确性。

    存在从 double 到 int 的 explicit 转换,但没有 implicit 转换。原因是,如果你在 int 中存储一个双精度值,那么当它去掉小数位时就会丢失值。

    MSDN 有一篇关于转化的好文章:http://msdn.microsoft.com/en-us/library/ms173105.aspx

    【讨论】:

      【解决方案4】:

      int 可以隐式转换为 double。这就是这里发生的事情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        • 2014-02-23
        • 2023-01-19
        • 1970-01-01
        相关资源
        最近更新 更多