简体   繁体   English

C++ if 语句总是返回真

[英]c++ if statements always return true

I have a bunch of maths manipulations that have thresholds, yet no matter what I change, the if statements always return true.我有一堆有阈值的数学操作,但无论我改变什么,if 语句总是返回 true。 No compile errors, can't get the debugger to work.没有编译错误,无法让调试器工作。 This is a function, the XY and Z arrays are all correct (I printed them to check earlier), the maths is right at least for the blade distance check, yet that always returns true.这是一个函数,XY 和 Z 数组都是正确的(我打印它们是为了更早检查),数学至少对于刀片距离检查是正确的,但总是返回 true。 I ran the same code (rewritten obviously) through matlab and that returns true or false depending on my data, so clearly its something with the way I've written this that's wrong.我通过 matlab 运行了相同的代码(显然是重写的),它根据我的数据返回 true 或 false,所以很明显它与我写这个的方式有问题。 Also is there any way to slimline this?还有什么办法可以使这个变薄吗?

bool Device::_SafeD(char _Type, float _Data[20][3]) {
    bool S;
    double x[20], y[20], z[20];
    for (int i=0; i<20; i++) {
        x[i] = _Data[i][0];
        y[i] = _Data[i][1];
        z[i] = _Data[i][2];
    }

    // Check angles for needle
    if (_Type == 'n') {
        for (int i=0; i<20; i++) {
            float dot, moda, modb, c, angle;
            dot = ((x[i]*x[i+1]) + (y[i]*y[i+1]) + (z[i]*z[i+1]));
            moda = sqrt(pow(x[i],2)+pow(y[i],2)+pow(z[i],2));
            modb = sqrt(pow(x[i+1],2)+(y[i+1],2)+(z[i+1],2));
            c = dot/(moda*modb);
            angle = acos(c);

            if (angle > 45){
                S = 0;
            } else {
                S = 1;
            }
        }
    }

    // Check distance for blade
    if (_Type == 'b'){
        for (int i=0; i<19; i++) {
            float distance = (x[i+1]-x[i]) + (y[i+1]-y[i]) + (z[i+1]-z[i]);
            cout << "distance " << distance << endl;

            if (distance > 5.0) {
                S  = 0;
            } else {
                S =  1;
            }
        }
    }
    if (S == 0) {
        return 0;
    }
    if(S == 1) {
        return 1;
    }
}

Cheers干杯

The most likely error is that you are comparing angle to an angle in degree while the return value of acos is in radians.最可能的错误是您将angle与角度进行比较,而acos的返回值以弧度为单位。

        if (angle > 45){

Convert the angle to degrees before comparing to 45 and you should be OK.在与 45 比较之前将角度转换为度数,您应该没问题。

        if (radians_to_degrees(angle) > 45){

where在哪里

double radians_to_degrees(double in)
{
    return (in*180/M_PI);
}

Another option is to compute the equivalent of 45 degrees in radians and compare it with angle .另一种选择是以弧度计算 45 度的等效值并将其与angle进行比较。

double const radian_45 = M_PI*45.0/180;

and use并使用

        if (angle > radian_45){

The other error is spelled out clearly in a comment by @Bob__: @Bob__ 在评论中清楚地说明了另一个错误:

OP uses two loops to check the angle and the distance, inside those loops a flag ( S ) is set to 0 or 1 , but it's done for every index in the array, so it's overwritten. OP 使用两个循环来检查角度和距离,在这些循环中,一个标志 ( S ) 被设置为01 ,但它对数组中的每个索引都进行了检查,因此它被覆盖了。 The return value of the entire function (in the provided code) depends only by the last two elements in the array.整个函数的返回值(在提供的代码中)仅取决于数组中的最后两个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM