Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • you subtract two unsigned types so the result is stored in an unsigned temporary too. try abs((int16_t) value - this->value) Commented Mar 25, 2023 at 10:16
  • 1
    @Juraj: It should be either abs((int16_t) (value - this->value)) or abs((int16_t) value - (int16_t) this->value). Or, better yet, make everything signed. Otherwise, if only one term of the subtraction is signed, the unsigned type wins the “usual arithmetic conversions” game. Commented Mar 25, 2023 at 11:17
  • @EdgarBonet Thank you, the weird behaviour was caused by uint16. I tested running the code (with uint) in VisualStudio and there it worked. Do you know why it worked in VS/Win11 but not in Arduino? Is the c++ version different? Compiler? And why didn't the compiler warn me? Ps, I am a complete beginner in c++ Commented Mar 25, 2023 at 12:55