为什么std ::圆形不是整数的返回值类型?

发布于 2025-01-30 05:41:43 字数 1573 浏览 5 评论 0原文

我对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)

在这里,我不明白为什么返回值类型floatdoublelong double double?例如,如果我有一个变量float x2.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2025-02-06 05:41:44

为什么std :: round的返回值类型不是整数?

因为浮点类型的可代表范围通常超过基本整数类型的范围,因此这些整数类型不能代表所有潜在的回报值。

除了超过整数类型范围的有限圆形值外,其他有问题的值是无限元,不是数字值,零值是返回的,而无需更改。

圆形值是3。可以使用int表示,对吧?

可以使用int表示此值。但是能够代表一个或某些值不够。返回类型应能够代表 all 圆形值。而且,通常无法使用int来实现。

在上面的代码中,int round_var_int = std :: round(var)很好,或者我需要明确地将其施放为int eg int eg int round_var_int = static_cast&lt; int&gt;( std :: round(var))避免任何潜在的错误(s)?


浮点类型隐式转换为整数类型。隐式转换的结果与静态铸件的结果相同。

这是一个狭窄的转换。从隐式转换也不是显式转换,从某种意义上说,如果浮点值不在代表范围之内,那么程序的行为是不确定的。两种转换都是安全的,从某种意义上说,如果值代表,则该值将保持不变。

Why is the return value type of std::round not an integer?

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.

rounded value is 3. That can be represented using just an int, right?

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 with int.

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)?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文