Explanation of the safe average of two numbers

前端 未结 7 1304
离开以前
离开以前 2021-02-08 14:26

Whenever I need to average two numbers for an algorithm like binary search, I always do something like this:

int mid = low + ((high - low) / 2);
<
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 15:00

    Program synthesis techniques appear to solve such problems.

    In this video, the programmer specifies constraints a) no overflow, b) no division, and c) no if-then-else. The synthesizer automatically came up with something pretty nice.

    https://youtu.be/jZ-mMprVVBU

提交回复
热议问题