How can I keep the result from equation in a range?

I want the value of dmg in this equation to be equal or greater than 0. Meaning, if the result is >0 then it gives the result only. But, if the result is <0 i.e. negative it will give 0. I know I can use 'if-else' but want to know if it can be done in one line without 'if-else'.

 
  dmg=int((a-(b/6))*mp);


Edit: I found the answer. I forgot about (x>y)?x:y
Last edited on
1
2
// https://en.cppreference.com/w/cpp/algorithm/max 
dmg = std::max( 0.0, int( (a-(b/6)) * mp ) ) ;
dmg= int((a-(b/6))*mp) * (int)(a>b/6); //can't jump. max hides a branch, ?: is a branch. Branch prediction is imperfect, so when its simple like this, avoidance is my preference. max is the pretty way to do it.

** technically it would be a>= but when its equal its still zero. either way.

** this works because zero. the expression becomes larger for general ranges and max becomes more attractive.
Last edited on
Topic archived. No new replies allowed.