我有以下简单代码:
int speed1 = (int)(6.2f * 10);
float tmp = 6.2f * 10;
int speed2 = (int)tmp;
speed1
和 speed2
应该有相同的值,但事实上,我有:
speed1 = 61
speed2 = 62
我知道我应该使用 Math.Round 而不是转换,但我想了解为什么值不同。
我查看了生成的字节码,但除了存储和加载之外,操作码是相同的。
我也在java中尝试了相同的代码,我正确地获得了62和62。
谁能解释一下?
编辑: 在真正的代码中,它不是直接 6.2f * 10 而是一个函数调用 * 一个常量。我有以下字节码:
对于 speed1
:
IL_01b3: ldloc.s V_8
IL_01b5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ba: ldc.r4 10.
IL_01bf: mul
IL_01c0: conv.i4
IL_01c1: stloc.s V_9
对于 speed2
:
IL_01c3: ldloc.s V_8
IL_01c5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ca: ldc.r4 10.
IL_01cf: mul
IL_01d0: stloc.s V_10
IL_01d2: ldloc.s V_10
IL_01d4: conv.i4
IL_01d5: stloc.s V_11
我们可以看到操作数是 float ,唯一的区别是 STLoc/ldloc
。
虚拟机方面,我试过Mono/Win7、Mono/MacOS、.NET/Windows,结果都一样。
最佳答案
首先,我假设您知道 6.2f * 10
由于浮点舍入而不是 62(当表示为 double
时,它实际上是值 61.99999809265137 >) 并且您的问题只是关于为什么两个看似相同的计算会导致错误的值。
答案是,在 (int)(6.2f * 10)
的情况下,您采用 double
值 61.99999809265137 并将其截断为整数,这产量 61。
在 float f = 6.2f * 10
的情况下,您将 double 值 61.99999809265137 和四舍五入到最接近的 float
,即 62。然后将 float
截断为整数,结果为 62。
练习:解释以下操作序列的结果。
double d = 6.2f * 10;
int tmp2 = (int)d;
// evaluate tmp2
更新:如注释中所述,表达式 6.2f * 10
在形式上是一个 float
,因为第二个参数隐式转换为 float
这是better而不是隐式转换为 double
。
实际问题是编译器被允许(但不是必须)使用一个中间值 higher precision than the formal type (section 11.2.2) .这就是为什么您会在不同的系统上看到不同的行为:在表达式 (int)(6.2f * 10)
中,编译器可以选择保留值 6.2f * 10
在转换为 int
之前以高精度中间形式。如果是,则结果为 61。如果不是,则结果为 62。
在第二个示例中,对 float
的显式赋值会强制在转换为整数之前进行舍入。
关于C# 浮点表达式 : strange behavior when casting the result float to int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8911440/