What is the right way to find the average of two values?

后端 未结 8 1168
北荒
北荒 2021-02-19 00:58

I recently learned that integer overflow is an undefined behavior in C (side question - is it also UB in C++?)

Often in C programming you need to find the average of two

8条回答
  •  星月不相逢
    2021-02-19 01:06

    If you are concerned about overflow, you could cast the values to a larger type to perform the math, and then do the bounds checking.

提交回复
热议问题