将变量转换为int vs round()函数

时间:2021-08-26 04:59:43

I have seen it in several places where (int)someValue has been inaccurate and instead the problem called for the round() function. What is the difference between the two?

我在一些地方见过它,在那里(int)someValue是不准确的,相反,问题需要round()函数。这两者之间有什么区别?

Specifically, if need be, for C99 C. I have also seen the same issue in my programs when writing them in java.

具体地说,如果需要的话,对于C99 c,我在用java编写程序时也遇到过同样的问题。

3 个解决方案

#1


14  

In case of casting a float/double value to int, you generally loose the fractional part due to integer truncation.

如果将浮点/双值转换为整型,由于整数截断,通常会使小数部分松脱。

This is quite different from rounding as we would usually expect, so for instance 2.8 ends up as 2 with integer truncation, just as 2.1 would end up as 2.

这与我们通常期望的舍入是完全不同的,因此,例如2.8以整数截断的形式结束为2,就像2.1以2结束一样。

Update:

更新:

Another source of potential (gross) inaccuracy with casting is due to the limited range of values being able to be represented with integers as opposed to with floating point types (thanks to @R reminding us about this in the comment below)

另一个潜在的(严重的)不准确的原因是由于可以用整数表示的值范围有限,而不是浮点类型(感谢@R在下面的注释中提醒我们)

#2


6  

  1. Casting to int truncates a floating-point number, that is, it drops the fractional part.
  2. 转换为int将截断浮点数,即去掉小数部分。
  3. The round function returns the nearest integer. Halfway cases are rounded away from zero, for example, round(-1.5) is -2 and round(1.5) is 2.
  4. round函数返回最近的整数。半圆的情况是从0四舍五入,例如,圆(-1.5)是-2,圆(1.5)是2。

7.12.9.6 The round functions

7.12.9.6圆函数

Synopsis

剧情简介

#include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);

Description

描述

The round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction.

round函数以浮点格式将参数四舍五入到最接近的整数值,不考虑当前的四舍五入方向,在离零的半圆上四舍五入。

Returns

返回

The round functions return the rounded integer value.

圆形函数返回整数值。

Source: the C99 standard (ISO/IEC 9899:1999). This section did not change in the C11 standard (ISO/IEC 9899:2011).

来源:C99标准(ISO/ iec9899:1999)。本节没有改变C11标准(ISO/IEC 9899:2011)。

(For those who are interested, here is a clear introduction to rounding algorithms.)

(对于感兴趣的人,这里有一个关于舍入算法的清晰介绍。)

#3


1  

Specifically for C, it is (probably) true in most cases that casting truncates, however, you should always test the outcome to be sure.

特别是对于C,在大多数情况下,它(可能)是正确的,但是,您应该总是测试结果以确定。

The problem with casting is, that it can EITHER truncate OR round. What it does depends largely on the programming language, marginally on the specific compiler used. Which means there is no general rule for the outcome that will always apply.

铸造的问题是,它可以截断或旋转。它的作用很大程度上取决于编程语言,略微取决于所使用的特定编译器。这意味着对结果没有一个通用的规则总是适用的。

Round() is universally used to generate a result similar to mathmatical rounding. There is an edge case that needs to be specifically monitored for .5, which sometimes rounds to the next even number.

Round()被普遍用于生成类似于mathmatical四舍五入的结果。有一个需要特别监控的边缘情况。5,有时甚至是下一个偶数。

#1


14  

In case of casting a float/double value to int, you generally loose the fractional part due to integer truncation.

如果将浮点/双值转换为整型,由于整数截断,通常会使小数部分松脱。

This is quite different from rounding as we would usually expect, so for instance 2.8 ends up as 2 with integer truncation, just as 2.1 would end up as 2.

这与我们通常期望的舍入是完全不同的,因此,例如2.8以整数截断的形式结束为2,就像2.1以2结束一样。

Update:

更新:

Another source of potential (gross) inaccuracy with casting is due to the limited range of values being able to be represented with integers as opposed to with floating point types (thanks to @R reminding us about this in the comment below)

另一个潜在的(严重的)不准确的原因是由于可以用整数表示的值范围有限,而不是浮点类型(感谢@R在下面的注释中提醒我们)

#2


6  

  1. Casting to int truncates a floating-point number, that is, it drops the fractional part.
  2. 转换为int将截断浮点数,即去掉小数部分。
  3. The round function returns the nearest integer. Halfway cases are rounded away from zero, for example, round(-1.5) is -2 and round(1.5) is 2.
  4. round函数返回最近的整数。半圆的情况是从0四舍五入,例如,圆(-1.5)是-2,圆(1.5)是2。

7.12.9.6 The round functions

7.12.9.6圆函数

Synopsis

剧情简介

#include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);

Description

描述

The round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction.

round函数以浮点格式将参数四舍五入到最接近的整数值,不考虑当前的四舍五入方向,在离零的半圆上四舍五入。

Returns

返回

The round functions return the rounded integer value.

圆形函数返回整数值。

Source: the C99 standard (ISO/IEC 9899:1999). This section did not change in the C11 standard (ISO/IEC 9899:2011).

来源:C99标准(ISO/ iec9899:1999)。本节没有改变C11标准(ISO/IEC 9899:2011)。

(For those who are interested, here is a clear introduction to rounding algorithms.)

(对于感兴趣的人,这里有一个关于舍入算法的清晰介绍。)

#3


1  

Specifically for C, it is (probably) true in most cases that casting truncates, however, you should always test the outcome to be sure.

特别是对于C,在大多数情况下,它(可能)是正确的,但是,您应该总是测试结果以确定。

The problem with casting is, that it can EITHER truncate OR round. What it does depends largely on the programming language, marginally on the specific compiler used. Which means there is no general rule for the outcome that will always apply.

铸造的问题是,它可以截断或旋转。它的作用很大程度上取决于编程语言,略微取决于所使用的特定编译器。这意味着对结果没有一个通用的规则总是适用的。

Round() is universally used to generate a result similar to mathmatical rounding. There is an edge case that needs to be specifically monitored for .5, which sometimes rounds to the next even number.

Round()被普遍用于生成类似于mathmatical四舍五入的结果。有一个需要特别监控的边缘情况。5,有时甚至是下一个偶数。