int overflow 和 double 的關係
(高顯忠, sjgau4311@gmail.com, 2012-02-14 08:23)
1樓
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
// ----------------------------------------------
// a= rnd1();
int rnd1(void)
{
int a1, a2;
a1= rand();
a2= rand();
return((a1 << 15) + a2);
}// end of rnd1()
// ----------------------------------------------
int main(int argc, char *argv[])
{
int a, b, c;
double xc;
srand(time(NULL));
while (1) {
a= rnd1()%100000;
b= rnd1()%100000;
xc= ((double) a)*b;
c= a*b;
printf("\n\n\na= %d, b= %d \n", a, b);
printf("xc= %.3lf, \nc= %d \n", xc, c);
// --------------------------------------
if (xc > ((pow(2, 31)) - 1)) {
xc-= (pow(2, 32));
printf("\n *** \n");
printf("xc= %.3lf, \nc= %d \n", xc, c);
}
system("PAUSE");
}// end of while()
return EXIT_SUCCESS;
}// end of main()