C++ 将更改限制在最大值

C++ 将更改限制在最大值,c++,c,C++,C,假设我有: int currentVelocity; //can be negative int targetVelocity; //can also be negative void updateVelocity() // called every 100ms { int limit = 5; } 在每次迭代中,最大绝对变化为5时,如何使速度更接近目标速度 假设我的当前速度是-20,目标速度是-26 我的最大绝对增量是5 第一次调用updateVelocity()时,currentV

假设我有:

int currentVelocity; //can be negative
int targetVelocity; //can also be negative

void updateVelocity() // called every 100ms
{
   int limit = 5;
}
在每次迭代中,最大绝对变化为5时,如何使速度更接近目标速度

假设我的当前速度是-20,目标速度是-26

我的最大绝对增量是5

第一次调用updateVelocity()时,currentVelocity变为-25 再次调用时,currentVelocity为-26

除非目标速度改变,否则将永远如此

要做到这一点,必须向更新函数添加什么

谢谢

简单明了

int currentVelocity; //can be negative
int targetVelocity; //can also be negative

void updateVelocity() // called every 100ms
{
   int limit = 5;
   int delta = targetVelocity - currentVelocity;

   if (delta > limit)
        currentVelocity += limit;
   else if (delta < -limit)
        currentVelocity -= limit;
   else
        currentVelocity = targetVelocity;
}
int-currentVelocity//可能是负面的
int目标速度//也可以是负数
void updateVelocity()//每100ms调用一次
{
整数极限=5;
int delta=目标速度-当前速度;
如果(增量>限制)
电流速度+=极限;
否则如果(增量<-极限)
电流速度-=极限;
其他的
currentVelocity=目标速度;
}

我不知道为什么要尝试将速度作为int来使用,但现在开始

int change_this_frame = targetVelocity - currentVelocity;

if( change_this_frame > 5 )
{
    change_this_frame = 5;
}

if( change_this_frame  < -5 )
{
    change_this_frame = -5;
}

currentVelocity += change_this_frame;
int change\u此帧=targetVelocity-currentVelocity;
如果(更改此帧>5)
{
更改此帧=5;
}
如果(更改此帧<-5)
{
更改此帧=-5;
}
currentVelocity+=更改此帧;

在INT\u MIN和INT\u MAX范围内做一点工作

#include <limits.h>
int currentVelocity; //can be negative
int targetVelocity; //c

void updateCurrentVelocity() // called every 100ms
{
   int limit = 5;
   int currentVelocityLimit;

   if (targetVelocity > currentVelocity) {
     if (currentVelocity <= INT_MAX - limit) {
       currentVelocityLimit = currentVelocity + limit;
     }
     else {
       currentVelocityLimit = INT_MAX;
     }
     if (currentVelocityLimit > targetVelocity) 
       currentVelocityLimit = targetVelocity;
     currentVelocity = currentVelocityLimit;
   }
   else if (targetVelocity < currentVelocity) {
     if (currentVelocity >= INT_MIN + limit) {
       currentVelocityLimit = currentVelocity - limit;
     }
     else {
       currentVelocityLimit = INT_MIN;
     }
     if (currentVelocityLimit < targetVelocity) 
       currentVelocityLimit = targetVelocity;
     currentVelocity = currentVelocityLimit;
   }
}
#包括
电流速度//可能是负面的
int目标速度//C
void updateCurrentVelocity()//每100ms调用一次
{
整数极限=5;
int currentVelocityLit;
如果(目标速度>当前速度){
if(当前速度目标速度)
CurrentVelocityLit=目标速度;
currentVelocity=CurrentVelocityLit;
}
否则如果(目标速度<当前速度){
如果(当前速度>=INT\u MIN+极限){
currentVelocityLimit=currentVelocity-极限;
}
否则{
currentVelocityLimit=INT_MIN;
}
if(CurrentVelocityLit
@PawełStawarz我的想法是我的updateVelocity函数需要更改。这是不完整的。我认为这不是一个合适的问题。。。你直接问一段代码。这个问题有什么难的地方让你把它贴在这里?这似乎simple@FilipeGonçalve在嵌入式环境中,不允许UB,这并不是那么简单。一个简单的
int delta=currentVelocity-targetVelocity
很容易溢出-这与OP无关。@chux-Hum,说得好。是的,但看到公认的答案,我认为他并不关心溢出:p@OP,如果您不关心溢出,请使用Gassa解决方案。