大数相乘?或 相加?
- C/C++ code
myCircle CPylonBackCheckView::FittingCircle(vector<int> XX, vector<int> YY , int NUM ){ myCircle circle; int x1 = 0,x2 = 0,x3 = 0; int y1 = 0,y2 = 0,y3 = 0; int x1y1 = 0,x1y2 = 0,x2y1 = 0; int xtemp = 0 ; for (int i = 0; i < NUM ; i++) { x1 = x1 + XX[i]; x2 = x2 + XX[i]*XX[i]; x3 = x3 + XX[i]*XX[i]*XX[i]; y1 = y1 + YY[i]; y2 = y2 + YY[i]*YY[i]; y3 = y3 + YY[i]*YY[i]*YY[i]; x1y1 = x1y1 + XX[i]*YY[i]; x1y2 = x1y2 + XX[i]*YY[i]*YY[i]; x2y1 = x2y1 + XX[i]*XX[i]*YY[i]; } long double C = NUM * x2 - x1 * x1; long double D = NUM * x1y1 - x1 * y1; long double E = NUM * x3 + NUM * x1y2 - (x2 + y2) * x1; long double G = NUM * y2 - y1 * y1; long double H = NUM * x2y1 + NUM * y3 - (x2 + y2) * y1; long double a = (H * D - E * G)/(C * G - D * D); long double b = (H * C - E * D)/(D * D - G * C); long double c = -(a * x1 + b * y1 + x2 + y2)/NUM; double A = a/(-2); //x坐标 double B = b/(-2); //y坐标 double R = sqrt(a * a + b * b - 4 * c)/2; circle.xCenter = int(A); circle.yCenter = int(B); circle.Radius = int(R); return circle;}以上代码for循环里面的内容在matlab里能够正确的计算出结果。但是在VC中,x3,y3,x1y2,x2y1计算出来的结果却不正常了。只能有9位,偶尔还有负值出现,XX[i],YY[i]的范围在0~1000;三个连乘最大也就是10的9次方,9个数位。调试发现当y3 = 2128102546,继续加上一个28094464时,y3变成了负数-2138770286.是因为大数相加溢出嘛?可是只有十位数而已啊~~~~~
[解决办法]
数据类型该为 long double试试
[解决办法]
int int32 max value is 4,294,967,295
[解决办法]
int的范围本来就是-2147418112至2147418112
2128102546+28094464 = 2156197010 超过了,所以显示负数
[解决办法]
[解决办法]
用DWORD或者DWORD64可以拓展正数的位数哟