开发者

Nested logical && operator permissable?

I have written a .oct function for use in Octave that is not giving the result I expected and I think the problem might lie with the snippet of code given below - is it OK to nest the logical && operator as I have done or does it need to be separated out using further internal if loops?

 if ((( final_side_PDF > final_uwr_PDF && final_side_PDF > final_unr_PDF ) && final_side_PDF > final_dwr_PDF ) && final_side_PDF > final_dnr_PDF)
    {
    market_model(ii) = 0.0;
    }
 else if ((( final_uwr_PDF > final_side_PDF && final_uwr_PDF > final_unr_PDF ) && final_uwr_PDF > final_dwr_PDF ) && final_uwr_PDF > final_dnr_PDF )
    {
    market_model(ii) = 1.0;
    }
 else if ((( final_unr_PDF > final_side_PDF && final_unr_PDF > final_uwr_PDF ) && final_unr_PDF > final_dwr_PDF ) && final_unr_PDF > final_dnr_PDF)
    {
    market_model(ii) = 2.0;
    }
 else if ((( final_dwr_PDF > final_side_PDF && final_dwr_PDF > final_uwr_PDF ) && final_dwr_PDF > final_unr_PDF ) && final_dwr_PDF >开发者_如何学JAVA final_dnr_PDF)
    {
    market_model(ii) = -1.0;
    }
 else if ((( final_dnr_PDF > final_side_PDF && final_dnr_PDF > final_uwr_PDF ) && final_dnr_PDF > final_unr_PDF ) && final_dnr_PDF > final_dwr_PDF)
    {
    market_model(ii) = -2.0;
    }
 else
    {
    market_model(ii) = market_model(ii-1);
    }

Edit in response to comments

I wish to check which one of the five variables; final_side_PDF, final_uwr_PDF, final_unr_PDF, final_dwr_PDF and final_dnr_PDF; has the largest value and to return a unique identifying number; 0,1,2,-1,-2; dependent on which one is the max. The above code is contained in a main (ii) loop.


Suggestion:

 typedef int final_t;

 // From your code I assume that "final_xx" can have the same values, otherwise a std::map would be simpler. 
 std::multimap<final_t, double> finals;

 finals.insert(std::make_pair(final_side_PDF, 0.0));
 finals.insert(std::make_pair(final_uwr_PDF,  1.0));
 finals.insert(std::make_pair(final_unr_PDF,  2.0));
 finals.insert(std::make_pair(final_dwr_PDF, -1.0));
 finals.insert(std::make_pair(final_dnr_PDF, -2.0));

 auto max_final = finals.rbegin();

 if(finals.count(max_final->first) == 1)
    masket_model(ii) = max_final->second;
 else
    market_model(ii) = market_model(ii-1);

If "final_xx" all have unique values you can make it even simpler:

 typedef int final_t;

 std::map<final_t, double> finals;

 finals[final_side_PDF] =  0.0;
 finals[final_uwr_PDF]  =  1.0;
 finals[final_unr_PDF]  =  2.0;
 finals[final_dwr_PDF]  = -1.0;
 finals[final_dnr_PDF]  = -2.0;

 auto max_final = finals.rbegin();

 market_model(ii) = max_final->second;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜