解決我得出了一些有趣的發現一個錯誤。C#錯誤操作員%
這個過程的結果
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();
}
是
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
立方根是4.爲什麼在這種情況下4 % 1 = 1
?
退房谷歌:(64 ^(1/3))MOD 1 – SQLMason
如果你想確保這是一個你得到模數的整數,試着去模擬它。 – SQLMason
您正在尋找這個我覺得 http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c – Nevyn