This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28377D: Incorrect output of the conditional expression with ternary operator

Part Number: TMS320F28377D


Hi

I find the conditional expression, "long NSOTLimit = (AbsPosLimit < LONG_MIN) ? (long)LONG_MIN : (long)AbsPosLimit;" could cause incorrect results.

AbsPosLimit is composed of AbsoluteCounter and IncPosLimit.

long long AbsPosLimit = (long long)AbsoluteCounter + (long long)IncPosLimit;

There are two cases to discuss :

Case 1

There are the calculation results of below codes :

AbsPosLimit = 8426644

NSOTLimit = LONG_MIN = -2147483648 (not expected)

AbsPosLimit is greater than LONG_MIN, so the value of NSOTLimit shouldn't be LONG_MIN. NSOTLimit is expected to be equal to AbsPosLimit.

#define LONG_MIN	0xFFFFFFFF80000000		//-2147483648

long AbsoluteCounter = 108397879;
long IncPosLimit = -99971235;

// calculate absolute position limit. use LL to check overflow
long long AbsPosLimit = (long long)AbsoluteCounter + (long long)IncPosLimit;


// if AbsPosLimit < LONG_MIN means overflow
long NSOTLimit = (AbsPosLimit < LONG_MIN) ? (long)LONG_MIN : (long)AbsPosLimit;

Case 2

To figure out the problem in Case 1, I try to set 

long AbsoluteCounter = 99971234;

long IncPosLimit = -99971235;

Then I get the results : 

AbsPosLimit = -1

NSOTLimit = -1 (expected)

The testing results show the correct value of NSOTLimit.

How can I explain why I can't get correct NSOTLimit result in Case 1 by executing the same conditional expression?

Thank you!