知識社群登入
位置: AutoCAD開放式教學 > 討論區 > 討論
整數的 over- flow
1樓
#if 0

// no= 81920 的時候,
sum <> xs, 

xs 是正確的目標答案,33億 左右,超過 +2147483647,
所以,出現 整數 over- flow 的錯誤。

no= 81920
sum= -939483136,
 xs= 3355484160
 *** Press [Esc] key for Stop! or other key for Continue...
// --------------------------------------------------------

no= 10
sum= 55,
 xs= 55
 *** Press [Esc] key for Stop! or other key for Continue...

no= 20
sum= 210,
 xs= 210
 *** Press [Esc] key for Stop! or other key for Continue...

no= 40
sum= 820,
 xs= 820
 *** Press [Esc] key for Stop! or other key for Continue...

no= 80
sum= 3240,
 xs= 3240
 *** Press [Esc] key for Stop! or other key for Continue...

no= 160
sum= 12880,
 xs= 12880
 *** Press [Esc] key for Stop! or other key for Continue...

no= 320
sum= 51360,
 xs= 51360
 *** Press [Esc] key for Stop! or other key for Continue...

no= 640
sum= 205120,
 xs= 205120
 *** Press [Esc] key for Stop! or other key for Continue...

no= 1280
sum= 819840,
 xs= 819840
 *** Press [Esc] key for Stop! or other key for Continue...

no= 2560
sum= 3278080,
 xs= 3278080
 *** Press [Esc] key for Stop! or other key for Continue...

no= 5120
sum= 13109760,
 xs= 13109760
 *** Press [Esc] key for Stop! or other key for Continue...

no= 10240
sum= 52433920,
 xs= 52433920
 *** Press [Esc] key for Stop! or other key for Continue...

no= 20480
sum= 209725440,
 xs= 209725440
 *** Press [Esc] key for Stop! or other key for Continue...

no= 40960
sum= 838881280,
 xs= 838881280
 *** Press [Esc] key for Stop! or other key for Continue...

no= 81920
sum= -939483136,
 xs= 3355484160
 *** Press [Esc] key for Stop! or other key for Continue...

#endif
// ----------------------------------------------

#include <stdio.h>
#include <math.h>
#include <process.h>
#include <conio.h>

#include "sj-01.h"
// ----------------------------------------------

// for time1(), time2()
int main()
{
int no, sum, i;
double xs;
no= 10;
while (no > 0) {
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
xs= (1.0 + no)*(no - 1.0 + 1.0)/2.0;
skip(1);
printf("no= %d \n", no);
printf("sum= %d, \n xs= %.0lf \n", sum, xs);
pause();
no*= 2;
}
return(0);
}// end of main()