主题:请教C++语言计算精度的问题
源代码如下:
#include<iostream.h>
#include<math.h>
int turnType = 0;
double temp2 = 0;
double temp3 = 0;
double temp4 = 0;
double temp5 = 0;
double temp6 = 0;
double temp7 = 0;
double temp8 = 0;
double toolRadius = 0;
double X_previous = 0;
double Y_previous = 0;
double X_current = 0;
double Y_current = 0;
double X_next = 0;
double Y_next = 0;
double i = 0;
double j = 0;
double lineHorizontalVector(double m, double n, double i, double j)
{
double temp = 0;
temp = (i - m) / sqrt(pow((i - m),2) + pow((j - n),2));
return temp;
}
double lineVerticalVector(double m, double n, double i, double j)
{
double temp = 0;
temp = (j - n) / sqrt(pow((i - m),2) + pow((j - n),2));
return temp;
}
double circleHorizontalVector(double m, double n)
{
double temp = 0;
temp = -1 * n / sqrt(pow(m, 2) + pow(n, 2));
return temp;
}
double circleVerticalVector(double m, double n)
{
double temp = 0;
temp = m / sqrt(pow(m, 2) + pow(n, 2));
return temp;
}
int sign(double m)
{
int temp = 0;
if (m > 0)
temp = 1;
else if (m < 0)
temp = -1;
else
temp = 0;
return temp;
}
int judgeTurnType(double m, double n, double i, double j)
{
int temp = 0;
int k = 0;
k = sign(toolRadius);
if (k * (j * m - i * n) >= 0)
temp = 1;
else
{
if ((m * i + n * j) >= 0)
temp = 2;
else
temp = 3;
}
cout << "结果为:" << k * (j * m - i * n) << endl;
return temp;
}
int main()
{
cout << "X_previous = ";
cin >> X_previous;
cout << "Y_previous = ";
cin >> Y_previous;
cout << "X_current = ";
cin >> X_current;
cout << "Y_current = ";
cin >> Y_current;
cout << "X_next = ";
cin >> X_next;
cout << "Y_next = ";
cin >> Y_next;
cout << "i = ";
cin >> i;
cout << "j = ";
cin >> j;
cout << "toolRadius = ";
cin >> toolRadius;
temp2 = lineHorizontalVector(X_current, Y_current, X_next, Y_next);
temp3 = lineVerticalVector(X_current, Y_current, X_next, Y_next);
temp4 = X_previous + i - X_current;
temp5 = Y_previous + j - Y_current;
temp6 = circleHorizontalVector(temp4, temp5);
temp7 = circleVerticalVector(temp4, temp5);
temp8 = sqrt(pow(i, 2) + pow(j, 2));
turnType = judgeTurnType(temp6, temp7, temp2, temp3);
cout << "temp2=" << temp2 << endl;
cout << "temp3=" << temp3 << endl;
cout << "temp6=" << temp6 << endl;
cout << "temp7=" << temp7 << endl;
cout << "temp8=" << temp8 << endl;
cout << "turnType=" << turnType << endl;
return 0;
}
该程序用于计算数控机床C刀补之圆弧接直线转接点坐标
输入(原本小数点后有很多位,我按照实际操作的情况精确到了0.0001):
X_previous = 126.8328
Y_previous = 3.6656
X_current = 100
Y_current = -50
X_next = 0
Y_next = 0
i = -13.4164
j = -26.8328
toolRadius = -10
结果导致输出结果为:
结果为:-2.77556e-016
temp2=-0.894427
temp3=0.447214
temp6=-0.894427
temp7=0.447214
temp8=30
turnType=2
按照结果中的数据
“temp2=-0.894427
temp3=0.447214
temp6=-0.894427
temp7=0.447214”
在子函数judgeTurnType(temp6, temp7, temp2, temp3)中k * (j * m - i * n)应该等于0,
可是输出“结果为:-2.77556e-016”为什么呢?
该怎么办才能让temp2、temp3、temp6、temp7、temp8精确到一定的位数,而不是系统默认的位数呢?
我期待的结果是等于0,该怎么修改程序呢?
[img]F:\请教的问题\运行界面.bmp[/img]
#include<iostream.h>
#include<math.h>
int turnType = 0;
double temp2 = 0;
double temp3 = 0;
double temp4 = 0;
double temp5 = 0;
double temp6 = 0;
double temp7 = 0;
double temp8 = 0;
double toolRadius = 0;
double X_previous = 0;
double Y_previous = 0;
double X_current = 0;
double Y_current = 0;
double X_next = 0;
double Y_next = 0;
double i = 0;
double j = 0;
double lineHorizontalVector(double m, double n, double i, double j)
{
double temp = 0;
temp = (i - m) / sqrt(pow((i - m),2) + pow((j - n),2));
return temp;
}
double lineVerticalVector(double m, double n, double i, double j)
{
double temp = 0;
temp = (j - n) / sqrt(pow((i - m),2) + pow((j - n),2));
return temp;
}
double circleHorizontalVector(double m, double n)
{
double temp = 0;
temp = -1 * n / sqrt(pow(m, 2) + pow(n, 2));
return temp;
}
double circleVerticalVector(double m, double n)
{
double temp = 0;
temp = m / sqrt(pow(m, 2) + pow(n, 2));
return temp;
}
int sign(double m)
{
int temp = 0;
if (m > 0)
temp = 1;
else if (m < 0)
temp = -1;
else
temp = 0;
return temp;
}
int judgeTurnType(double m, double n, double i, double j)
{
int temp = 0;
int k = 0;
k = sign(toolRadius);
if (k * (j * m - i * n) >= 0)
temp = 1;
else
{
if ((m * i + n * j) >= 0)
temp = 2;
else
temp = 3;
}
cout << "结果为:" << k * (j * m - i * n) << endl;
return temp;
}
int main()
{
cout << "X_previous = ";
cin >> X_previous;
cout << "Y_previous = ";
cin >> Y_previous;
cout << "X_current = ";
cin >> X_current;
cout << "Y_current = ";
cin >> Y_current;
cout << "X_next = ";
cin >> X_next;
cout << "Y_next = ";
cin >> Y_next;
cout << "i = ";
cin >> i;
cout << "j = ";
cin >> j;
cout << "toolRadius = ";
cin >> toolRadius;
temp2 = lineHorizontalVector(X_current, Y_current, X_next, Y_next);
temp3 = lineVerticalVector(X_current, Y_current, X_next, Y_next);
temp4 = X_previous + i - X_current;
temp5 = Y_previous + j - Y_current;
temp6 = circleHorizontalVector(temp4, temp5);
temp7 = circleVerticalVector(temp4, temp5);
temp8 = sqrt(pow(i, 2) + pow(j, 2));
turnType = judgeTurnType(temp6, temp7, temp2, temp3);
cout << "temp2=" << temp2 << endl;
cout << "temp3=" << temp3 << endl;
cout << "temp6=" << temp6 << endl;
cout << "temp7=" << temp7 << endl;
cout << "temp8=" << temp8 << endl;
cout << "turnType=" << turnType << endl;
return 0;
}
该程序用于计算数控机床C刀补之圆弧接直线转接点坐标
输入(原本小数点后有很多位,我按照实际操作的情况精确到了0.0001):
X_previous = 126.8328
Y_previous = 3.6656
X_current = 100
Y_current = -50
X_next = 0
Y_next = 0
i = -13.4164
j = -26.8328
toolRadius = -10
结果导致输出结果为:
结果为:-2.77556e-016
temp2=-0.894427
temp3=0.447214
temp6=-0.894427
temp7=0.447214
temp8=30
turnType=2
按照结果中的数据
“temp2=-0.894427
temp3=0.447214
temp6=-0.894427
temp7=0.447214”
在子函数judgeTurnType(temp6, temp7, temp2, temp3)中k * (j * m - i * n)应该等于0,
可是输出“结果为:-2.77556e-016”为什么呢?
该怎么办才能让temp2、temp3、temp6、temp7、temp8精确到一定的位数,而不是系统默认的位数呢?
我期待的结果是等于0,该怎么修改程序呢?
[img]F:\请教的问题\运行界面.bmp[/img]