C:四舍五入到最接近的倍数
这感觉像是一个基本问题,但到目前为止我找不到明确的答案。
我想实现一个高效的函数round_to_nearest(int x, int multiple),将有符号整数四舍五入x到最接近的倍数multiple,尽可能避免使用浮点算法。
示例输出:
round_to_nearest(14, 5);
15
round_to_nearest(16, 5);
15
round_to_nearest(23, 5);
25
round_to_nearest(22, 5);
20
round_to_nearest(-23, 5);
-25
round_to_nearest(-22, 5);
-20