回 帖 发 新 帖 刷新版面

主题:请教一个关于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

[img]F:\请教的问题\运行界面.bmp[/img]
按照结果中的数据
“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,该怎么修改程序呢?


回复列表 (共5个回复)

沙发

这是一个互助的模式。
不推广人也有钱赚的。分A B两个网。
不推广的人在A网。推广的人 在B网。
B网推广的人帮助A网。
A网拿6000元出局。
B网就多了。
C易特元亨QQ919169870
奖励制度 
  http://blog.sina.com.cn/s/blog_6b19ec920100liv6.html

板凳

寻找中国的最优秀的网商领袖精英 淘宝商盟元亨 qq: 908889846 
当今世界正经历着全球经济一体化的大潮,中国本土企业也因此面临着前所未有的机遇与挑战。
在这场洗礼中,哪些互联网平台有能力成为世界级的电子商务平台?网商精英要怎样做,才能最终成长为世界级网商精英领袖?
淘宝商盟平台震撼登场,携手淘宝30万商家联盟购物商城。
平台刚刚启动,互联网的网商精英请咨询qq: 908889846 
占领市场第一先机,合力打造网商系统!
淘宝商盟官网   www.taobaosm.com
 http://blog.sina.com.cn/tbsm8
淘宝商盟奖励制度

3 楼

那如果用的计算机是64位的,同样是双精度浮点数运算,精度会提高吗?

4 楼

楼主把结果中小于一定的数盼为零不就行了,
如果机床最小为0.0001,那么-2.77556e-016对于你来说不就是0吗?

5 楼

高精度计算的时候,尽量不要将比较小的数作分母,把公式重新推导下

我来回复

您尚未登录,请登录后再回复。点此登录或注册