Comparing two integers without any comparison

前端 未结 14 1814
温柔的废话
温柔的废话 2020-12-05 16:06

Is it possible to find the greatest of two integers without any comparison? I found some solutions:

if(!(a/b)) // if a is less than b then division result          


        
14条回答
  •  清歌不尽
    2020-12-05 16:39

    I just cant see any good reason to do that : who would want to program without "if" ?

    a possible answer is :

    ( ( a + b ) + abs ( a -b ) ) / 2

    I guess "abs" just hides a "if" somewhere, just as the ternary operator that is just another name for "if" ...

提交回复
热议问题