This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem With Subtraction

I am trying to do a straight forward subtraction and am getting the wrong result every time. The code is as simple as

if(SetPoint>PulseCountLast) { error = SetPoint-PulseCountLast; }

All three variables are defined as uint16.

As you can see I ensured that I dont get a negative error value. Although I did try using error as signed number but still the faced the same issue. In any case the issue is when debugging is that SetPoint has a value of 0x0064 and PulseCountLast has a value of 0x0000 so I would expect an error value of 0x0065, however, the code always results in an error value of 0x0001 no matter what value the operands have.

I am really confused at this point what it might be...I would appreciate some help.

Parents
  • I don't think you have given us all code relevant to help you.

    But anyway. If you do 0x0064 - 0x0000, you can't really expect the answer to be 0x0065.

    Another thing. Are you sure that your _real_ code doesn't do: error = SetPoint > PulseCountLast, in which case error will be zero or one since it will be assigned with the result of a comparison and not the result of a subtraction?

Reply
  • I don't think you have given us all code relevant to help you.

    But anyway. If you do 0x0064 - 0x0000, you can't really expect the answer to be 0x0065.

    Another thing. Are you sure that your _real_ code doesn't do: error = SetPoint > PulseCountLast, in which case error will be zero or one since it will be assigned with the result of a comparison and not the result of a subtraction?

Children