c - 用整数计算 float 的标准化方法是怎样的?

标签 c type-conversion type-promotion

你们知道这在 C 中是如何计算的吗?

uint8_t samplerate = 200;
uint8_t Result;
Result = 0.5 * samplerate;

现在,问题是 0.5 是一个 float ,而 samplerate一个整数。 Result然后可以是 0,因为 0.5 被转换为整数,因此四舍五入为 0 ( Result = 0 * 200 = 0 )。或者 Result可能是 100,因为编译器首先看到 0.5 并转换 samplerate变成 float (Result = 0.5 * 200 = 100)。

是否有编译器处理这些计算的标准化方式? 我的意思是编译器会首先查看最左边的变量(在本例中为 0.5)并将另一个变量转换为这个,还是会查看最右边的变量(samplerate)并将其他变量转换为这个?

我知道如何解决这个问题,但我正在寻找一个通用的答案,如果这是 C 标准化的,它将如何计算此类方程式?

最佳答案

当各种类型的数值组合在一个表达式中时,它们会受到通常的算术转换的约束,这是一组规定应将哪个操作数转换为哪种类型的规则。

这些转换在 C 标准的第 6.3.1.8 节中有详细说明:

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions :

  • First, if the corresponding real type of either operand is long double , the other operand is converted, without change of type domain, to a type whose corresponding real type is long double .
  • Otherwise, if the corresponding real type of either operand is double , the other operand is converted, without change of type domain, to a type whose corresponding real type is double .
  • Otherwise, if the corresponding real type of either operand is float , the other operand is converted, without change of type domain, to a type whose corresponding real type is float .
  • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
    • If both operands have the same type, then no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

请特别注意粗体段落,这适用于您的情况。

浮点常量 0.5 的类型为 double,因此其他操作数的值被转换为类型 double,并且乘法运算符 * 的类型为 double。然后将此结果赋值回 uint8_t 类型的变量,因此 double 值将转换为此类型以进行赋值。

所以在这种情况下,Result 的值为 100。

关于c - 用整数计算 float 的标准化方法是怎样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54481701/

相关文章:

c - C 中的右移运算符

c++ - c++中如何将字符串转换为string中提到的数据类型

python - 通过 IPython Notebook 转换更改 PDF-Latex 输出的样式

用于检查整数溢出的编译器标志

c++ - 可以为晋升制定类型特征吗?

c - 在 C 中运行程序时测量时间的最佳/最有效方法是什么?

c - 使 Visual Studio 项目文件与目录内容保持同步?

c - 反转字符串的库函数

c++ - 转换 - 错误 E2015,AnsiString(char) 和 AnsiString(short) 之间存在歧义

c++ - 当 int 类型的符号不匹配时的 g++ 错误消息