为什么std ::圆形不是整数的返回值类型?
我对float
/double
变量到最近的整数有些困惑。在谷歌搜索时,我遇到此用于舍入的cppreference页面。在该页面上,它说:
float round ( float arg ); // (1)
float roundf( float arg ); // (2)
double round ( double arg ); // (3)
long double round ( long double arg ); // (4)
double round ( IntegralType arg ); // (5)
在这里,我不明白为什么返回值类型float
,double
或long double double
?例如,如果我有一个变量float x
和2.5< = x< 3.5
其圆形值为3
。可以仅使用int
来表示,对吗?因此,为什么std :: Round
不是int
(或long IT
)的返回值类型)?
在该页面上,它还说:
1-5)计算最接近的整数值ARG(以浮点格式),无论当前的圆形模式如何
那么,的“整数值”(以浮点格式)是什么意思?
要理解此std ::圆形
,我进行了一个小实验:
int main(int argc, char const *argv[]) {
float var = 3.14;
int rounded_var_int = std::round(var);
float rounded_var_float = std::round(var);
std::cout << rounded_var_int << ", " << rounded_var_float << std::endl; // 3, 3
return 0;
}
但是,这使我更加困惑,因为一切都很好。
因此,最后一个问题:
在上面的代码中,int round_var_int = std :: round(var)
很好,或者我需要将其显式施放到int
eg eg int round_var_int = static_cast&lt ; int&gt;(std :: rond(var))
避免任何潜在的错误?
I had/have some confusion regarding rounding a float
/double
variable to the nearest integer. While googling, I came across this cppreference page for rounding. On that page, it says:
float round ( float arg ); // (1)
float roundf( float arg ); // (2)
double round ( double arg ); // (3)
long double round ( long double arg ); // (4)
double round ( IntegralType arg ); // (5)
Here, I don't understand why is the return value type float
, double
, or long double
? For example, if I have a variable float x
and 2.5 <= x < 3.5
then its rounded value is 3
. That can be represented using just an int
, right? So, why is the return value type of std::round
not an int
(or long it
for long double
)?
On that page, it also says:
1-5) Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
So, what does exactly "integer value to arg (in floating-point format)" mean?
To understand this std::round
, I did one small experiment:
int main(int argc, char const *argv[]) {
float var = 3.14;
int rounded_var_int = std::round(var);
float rounded_var_float = std::round(var);
std::cout << rounded_var_int << ", " << rounded_var_float << std::endl; // 3, 3
return 0;
}
However, this made me even more confused because everything ran just fine.
So, the last question:
In the above code, int rounded_var_int = std::round(var)
is just fine or do I need to explicitly cast it to int
e.g. int rounded_var_int = static_cast<int>(std::round(var))
to avoid any potential bug(s)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为浮点类型的可代表范围通常超过基本整数类型的范围,因此这些整数类型不能代表所有潜在的回报值。
除了超过整数类型范围的有限圆形值外,其他有问题的值是无限元,不是数字值,零值是返回的,而无需更改。
可以使用
int
表示此值。但是能够代表一个或某些值不够。返回类型应能够代表 all 圆形值。而且,通常无法使用int
来实现。浮点类型隐式转换为整数类型。隐式转换的结果与静态铸件的结果相同。
这是一个狭窄的转换。从隐式转换也不是显式转换,从某种意义上说,如果浮点值不在代表范围之内,那么程序的行为是不确定的。两种转换都是安全的,从某种意义上说,如果值代表,则该值将保持不变。
Because the representable range of floating point types generally exceed the range of fundamental integer types and thus those integer types cannot represent all potential return values.
Besides finite rounded values that exceed the range of integer types, other problematic values are infinities, not-a-number values, negative zero which are returned without change.
This value can be represented using
int
. But being able to represent one, or some of the values isn't sufficient. The return type should be able to represent all rounded values. And that isn't generally achievable withint
.Floating point types are implicitly convertible to integer types. The result of the implicit conversion is the same as the result of the static cast.
It is a narrowing conversion. Neither implicit nor explicit conversion is safe in the sense that if the floating point value is outside of representable range, then the behaviour of the program is undefined. Both conversions are safe in the sense that if the value is representable, then the value will remain unchanged by the conversion.