【问题标题】:.NET bug when converting float to integer?将浮点数转换为整数时的.NET 错误?
【发布时间】:2010-09-20 13:38:18
【问题描述】:

检查一下:这个小的 .NET 控制台程序产生了有趣的结果...注意我是如何以两种不同的方式将浮点数转换为整数的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CastVsConvert
{
    class Program
    {
        static void Main(string[] args)
        {
            int newWidth = 0;
            CalculateResizeSizes(600, 500, out newWidth);
        }

        static void CalculateResizeSizes(int originalWidth, int maxWidth, out int newWidth)
        {
            float percentage = 1.0F;
            percentage = maxWidth / (float)originalWidth;

            newWidth = (int)((float)originalWidth * percentage);
            int newWidthConvert = Convert.ToInt32((float)originalWidth * percentage);

            Console.Write("Percentage: {0}\n", percentage.ToString());
            Console.Write("Cast: {0}\n", newWidth.ToString());
            Console.Write("Convert: {0}\n", newWidthConvert.ToString());
        }
    }
}

我希望“Cast”和“Convert”的输出是相同的,但它们不是……输出如下:

C:\Documents and Settings\Scott\My Documents\Visual Studio 2008\Projects\CastVsC
onvert\CastVsConvert\bin\Debug>CastVsConvert.exe
Percentage: 0.8333333
Cast: 499
Convert: 500

有人知道为什么 .NET 在这里返回不同的值吗?

【问题讨论】:

    标签: .net floating-point


    【解决方案1】:

    这不是错误,cast 截断,convert 循环。

    this

    【讨论】:

      【解决方案2】:

      在四舍五入的同时,演员将截去小数点后的数字部分。

      【讨论】:

        【解决方案3】:

        来自Convert.ToInt32返回值的文档:

        四舍五入到最接近的 32 位有符号整数。如果值是一半 在两个整数之间,偶数 返回号码;也就是说,4.5 是 转换为 4,转换为 5.5 到 6。

        铸造不会四舍五入 - 它只是截断。乘法的结果略低于 500,因此强制转换会将其截断为 499,而 Convert.ToInt32 会将其四舍五入为 500。

        【讨论】:

          【解决方案4】:

          有一个额外的、隐藏的演员表可能是造成这种情况的原因。例如,如果您使用它而不是重新计算:

          int newWidthConvert = Convert.ToInt32(newWidth);
          

          你会得到同样的结果。当您使用 Reflector 查看 Convert.ToInt32(float) 时,发生的事情变得更加清晰:

          public static int ToInt32(float value)
          {
              return ToInt32((double) value);
          }
          

          Double 有一个隐藏的演员表。

          如果你添加几行来调查,然后使用调试器来看看,你会看到会发生什么:

          float newWidth1 = ((float)originalWidth * percentage);
          double newWidth2 = ((float)originalWidth * percentage); 
          

          double 更精确,并将值保存为 499.999999 和更多的十进制数字。 float 不太精确,存储 500.0。整数转换会截断小数部分,因此您最终会根据中间转换得到 500 或 499。当您调用Convert.ToInt32() 时,结果已经转换为float,因此您将获得500.0 的Double 表示。我个人更喜欢尽可能使用double

          【讨论】:

            猜你喜欢
            • 2018-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多