解决一个bug后,我发现了一些有趣的发现。
这个过程的结果是:
这个过程的结果是:
static void Main(string[] args)
{
int i4 = 4;
Console.WriteLine("int i4 = 4;");
Console.WriteLine("i4 % 1 = {0}", i4 % 1);
double d4 = 4.0;
Console.WriteLine("double d4 = 4.0;");
Console.WriteLine("d4 % 1 = {0}", d4 % 1);
Console.WriteLine("-----------------------------------------------------------");
int i64 = 64;
double dCubeRootOf64 = Math.Pow(i64, 1.0 / 3.0);
Console.WriteLine("int i64 = 64;");
Console.WriteLine("double dCubeRootOf64 = Math.Pow(i64, 1.0 / 3.0) = {0}", dCubeRootOf64);
Console.WriteLine("dCubeRootOf64 = {0}", dCubeRootOf64);
Console.WriteLine("dCubeRootOf64 % 1 = {0} ?????????????? Why 1. ??????????", dCubeRootOf64 % 1);
Console.ReadLine();
}
is
int i4 = 4;
i4 % 1 = 0
double d4 = 4.0;
d4 % 1 = 0
-----------------------------------------------------------
int i64 = 64;
double dCubeRootOf64 = Math.Pow(i64, 1.0 / 3.0) = 4
dCubeRootOf64 = 4
dCubeRootOf64 % 1 = 1 ?????????????? Why 1. ??????????
int 4 % 1 = 0
-- 正确
double 4.0 % 1 = 0
-- 正确
但错误出现在:
Math.Pow(64, 1.0 / 3.0) % 1 = 1
64的立方根是4。为什么这种情况下4%1=1
?
Math.Pow(x, 1.0 / 3.0)
并不能计算立方根,即使Pow是无限精确的(它并不是),因为1.0 / 3.0
不是三分之一 - 它是0.33333333333333331。 - harold