Mathematically Find Max Value without Conditional Comparison

前端 未结 14 589
暗喜
暗喜 2020-12-03 03:39

----------Updated ------------

codymanix and moonshadow have been a big help thus far. I was able to solve my problem using the equations and instead of using right

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 04:02

    Solution without conditionals. Cast to uint then back to int to get abs.

    int abs (a) { return (int)((unsigned int)a); }
    int max (a, b) { return (a + b + abs(a - b)) / 2; }
    
    int max3 (a, b, c) { return (max(max(a,b),c); }
    

提交回复
热议问题