作者 主題: 不曉得是否OS 32位元的問題!!  (閱讀 1996 次)

0 會員 與 1 訪客 正在閱讀本文。

cisco3662

  • 鑽研的研究生
  • *****
  • 文章數: 739
    • 檢視個人資料
不曉得是否OS 32位元的問題!!
« 於: 2011-03-04 12:04 »
如下,執行後變成負數...
代碼: [選擇]
[root@localhost control]# cat test
first=110000000000
second=111000000000
declare -i concurrent=($second-$first)*8/1024/180
echo "第二次$second"
echo "第一次$first"
declare -i test=($second-$first)
echo "$test"
echo "$concurrent"
[root@localhost control]# ./test
第二次111000000000
第一次110000000000
1000000000
-3200

Yamaka

  • 俺是博士!
  • *****
  • 文章數: 4913
    • 檢視個人資料
    • http://www.ecmagic.com
回覆: 不曉得是否OS 32位元的問題!!
« 回覆 #1 於: 2011-03-04 12:13 »
如下,執行後變成負數...
代碼: [選擇]
[root@localhost control]# cat test
first=110000000000
second=111000000000
declare -i concurrent=($second-$first)*8/1024/180
echo "第二次$second"
echo "第一次$first"
declare -i test=($second-$first)
echo "$test"
echo "$concurrent"
[root@localhost control]# ./test
第二次111000000000
第一次110000000000
1000000000
-3200

試一下去掉後面 2 個或 3 個 0 不就知道是不是數值太大的關係  :D

cisco3662

  • 鑽研的研究生
  • *****
  • 文章數: 739
    • 檢視個人資料
回覆: 不曉得是否OS 32位元的問題!!
« 回覆 #2 於: 2011-03-04 13:48 »
我會這樣說是因為
32位元,故2的31次方為2147483648(1024*1024*1024*2)
我刻意將兩次相減的值大於2147483648
得到結果為負數
故我才會懷疑是否32位元作業系統對於數字的處理最上限為2147483648

同樣的方式在64位元的linux上試驗是正常的

代碼: [選擇]
[root@localhost control]# cat test
first=2000000000
second=4200000000
declare -i concurrent=($second-$first)*8/1024/180
echo "第二次$second"
echo "第一次$first"
declare -i test=($second-$first)
echo "$test"
echo "$concurrent"
[root@localhost control]# ./test
第二次4200000000
第一次2000000000
-2094967296
2279
« 上次編輯: 2011-03-04 13:51 由 cisco3662 »