I saw this question:
Asking how to implement this code In a short and elegant way:
if speed > 0 then speed = speed - 20 * dt if speed < 0 then speed = 0 end end if speed < 0 then speed = speed + 20 * dt if speed > 0 then speed = 0 end end
This was my answer:
sign = (speed >= 0? 1 : -1); speed -= sign * 20 * dt // 20 is the rate of deccelaration if(sign != (speed >= 0? 1 : -1)) speed = 0;
It seems to meet the criteria presented by the question and it could be useful in platformers where you wish to deccelerate the chracter on the x- axis and control y-axis with gravity. It is also semantically identical to what the OP requested in the question.
Some people felt the answer was too localized to 1-d games and decided to downvote on that account.
Is that really a bad thing in answer, that it's written to best suit the question without adding more advance tecniques that were not required by the asker?