简体   繁体   English

"C ++中的整数舍入"

[英]Integer rounding in C++

I was trying to use the formula below in c++.我试图在 C++ 中使用下面的公式。 I have both variables declared as integers and I'm expecting them to round up but they seem to be rounding down.我将两个变量都声明为整数,我希望它们四舍五入,但它们似乎正在四舍五入。 I have looked over this but cannot seem to find what is wrong.我已经查看了这个,但似乎无法找到问题所在。 Any help would be greatly appreciated.任何帮助将不胜感激。

int user_degrees_latitude, user_degrees_longitude;
const int lat_const=(-90)
const int long_const=(-180)

sector_latitude = (user_degrees_latitude - lat_const) / (10);
sector_longitude = (user_degrees_longitude - long_const) / (10);

In C++, integers are not rounded. 在C ++中,整数不是四舍五入的。 Instead, integer division truncates (read: always rounds towards zero) the remainder of the division. 相反,整数除法截断(读取:始终向零舍入)除法的余数。

If you want to get a rounding effect for positive integers, you could write: 如果你想获得整数的舍入效果,你可以写:

sector_latitude = static_cast<int>(((user_degrees_latitude - lat_const) / (10.0)) + 0.5);

The addition of 0.5 causes the truncation to produce a rounding effect. 0.5的加法导致截断产生舍入效应。 Note the addition of the .0 on 10.0 to force a floating point divide before the addition. 注意在10.0上添加.0以在添加之前强制浮点除法。

I also assumed that sector_latitude was an int with the casting. 我还假设sector_latitude是一个铸造的int。

Integer division in C++ always rounds towards zero. C ++中的整数除法总是向零舍入。 Use floating-point division to get "exact" results and use std::round to round according to the normal rules: 使用浮点除法得到“精确”结果并使用std::round round按照正常规则进行舍入:

sector_latitude = static_cast</*type of sector_latitude*/>( std::round( (user_degrees_latitude - lat_const) / 10.0 ));

The "10.0" (a double ) instead of "10" (an int ) tells the compiler to use floating-point arithmetic. “10.0”( double )而不是“10”( int )告诉编译器使用浮点运算。 It always chooses floating-point over integer calculation if a floating-point value like a double is involved. 如果涉及像double这样的浮点值,它总是选择浮点数而不是整数计算。

Although this thread is very old, I am saddened to see both existing answers converting to floating point then back to integer.虽然这个线程很老,但我很遗憾看到两个现有的答案都转换为浮点数,然后又转换回整数。 If you want to divide n by d and round the quotient to the nearest integer, all using integer-only arithmetic, then you simply add d/2 before dividing:如果您想将n除以d并将商四舍五入到最接近的整数,所有这些都使用仅整数算术,那么您只需在除法之前添加d/2

q = (n + d/2) / d

(Of course, d/2 could be written d>>1 , but the compiler will do that for you.) So, if n is 19 and d is 4, the exact quotient would be 4.75, n/d would yield 4 (truncation towards zero), but the formula above would yield 5, which is the desired result. (当然, d/2可以写成d>>1 ,但编译器会为你这样做。)因此,如果n是 19 并且d是 4,则确切的商将是 4.75, n/d将产生 4 (向零截断),但上面的公式将产生 5,这是所需的结果。 For the example given in the OP, simply update the last two lines as follows:对于 OP 中给出的示例,只需将最后两行更新如下:

sector_latitude = (user_degrees_latitude - lat_const + 5) / (10);
sector_longitude = (user_degrees_longitude - long_const + 5) / (10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM