的問題是,文字「99」被視爲一個int。如果您添加「L」,它會將其視爲長時間。爲了解決您的編譯問題:
long test2 = 99L * 99L * 99L * 99L * 99L;
並修復「不正確的結果」所造成整數溢出:
long r = 99;
long test1 = r * r * r * r * r;
關鍵的一點是,體現出了「=」進行評估的權之前完成long r
的分配。
還有其他文字後綴你可能感興趣的:
Type Suffix Example
uint U or u 100U
long L or l 100L
ulong UL or ul 100UL
float F or f 123.45F
decimal M or m 123.45M
@ m.edmonson,關於你的,爲什麼它出來到919965907.這是怎麼回事的問題,是該值是「包裝」周圍int.MaxValue。你可以用一個小測試程序看到:
int i = 99; // 99
i *= 99; // 9801
i *= 99; // 970299
i *= 99; // 96059601
i *= 99; // 919965907 should be 9509900499 but comes out to 919965907
// which is (9509900499 % int.MaxValue)
long k = 9509900499 % int.MaxValue;
什麼意思是「環繞」?當您以1超過int.MaxValue
時,值「返回」到int.MinValue
。
int j = int.MaxValue;
j++;
bool isNowMinValue = (j == int.MinValue); // true, the value has "wrapped around"
這有點簡單化;如果你搜索「整數溢出」,你會得到一個更好的解釋。這是值得理解如何整數(和其他數字類型)與32個比特表示:
http://en.wikipedia.org/wiki/Signed_number_representations
優秀的答案 - 你能解釋一下爲什麼他在長整數乘法之間得到不同的結果? –
你能澄清你的意思嗎?那麼這與模數%有關? –
我相信你只需要一個'99L',剩下的就會變成upcast。 –