10

在 Steven Prata 的“C Primer Plus”一书中,有一个关于类型转换的部分,其中“基本规则是”部分已在规则 1 中说明:

在 K&R C 下,但不是在当前 C 下,float 会自动转换为 double。

http://www.9wy.net/onlinebook/CPrimerPlus5/ch05lev1sec5.html

有人能解释一下是什么but not under current C意思吗?是否有自动转换的 C 版本和不自动转换的版本?

我试图了解如果我有一个混合浮点数和双精度数的表达式,我可以依靠 C 在评估时将浮点数提升为双精度数吗?

4

5 回答 5

13

It must refer to the result of binary arithmetic operations of float * float format. In the pre-standard versions of C operands of such expressions were promoted to double and the result had double type.

For example, here's a quote from "C Reference Manual"

If both operands are int or char, the result is int. If both are float or double, the result is double.

In C89/90 already this behavior was changed and float * float expressions produce float result.

  • If either operand has type long double, the other operand is converted to long double
  • Otherwise, if either operand is double, the other operand is converted to double.
  • Otherwise, if either operand is float, the other operand is converted to float.
于 2012-11-16T22:09:39.537 回答
5

Look at the entire rule:

When appearing in an expression, char and short, both signed and unsigned, are automatically converted to int or, if necessary, to unsigned int. (If short is the same size as int, unsigned short is larger than int; in that case, unsigned short is converted to unsigned int.) Under K&R C, but not under current C, float is automatically converted to double. Because they are conversions to larger types, they are called promotions.

If we consider the integer types, when they appear in e.g. arithmetic expressions, they are still promoted, so no arithmetic is -theoretically - performed at the types char or short, but all at type int, unsigned int or a type with higher conversion rank (under the as-if rule, if the implementation can guarantee that the result is the same as if the promotion were actually carried out, it can perform arithmetic at smaller types if the platform provides the instructions).

The analogous used to hold for float, under the old pre-standard rules, floats were promoted to double for all arithmetic etc.

That is no longer the case, arithmetic on floats does not involve automatic promotion under standardised C.

In expressions with mixed types, generally everything is still promoted to the largest involved type, so if you compare or add a float to a double, the float is converted to double before the operation.

于 2012-11-16T22:08:47.777 回答
2

多年来,C 语言定义已多次标准化和修改。C 的原始(非标准化)版本被称为“K&R C”;这是 Kernighan 和 Ritchie 最初开发的语言。

1989 年,ANSI 创建了一个官方标准文档(ISO 于 1990 年采用)来定义语言,并且在该标准中进行了一些更改和扩展;其中一项更改是删除 了从floatto的自动升级。double

从那时起,该标准已经进行了两次修订,一次是在 1999 年,一次是在 2011 年。

我试图了解如果我有一个混合浮点数和双精度数的表达式,我可以依靠 C 在评估时将浮点数提升为双精度数吗?

这是当前标准的规则:

6.3.1.8 常用算术转换

...
首先,如果任一操作数的对应实类型为long double,则将另一个操作数转换为对应实类型为 的类型,而不改变类型域long double

否则,如果任一操作数的对应实类型为double,则将另一个操作数转换为对应实类型为 的类型,而不改变类型域double

否则,如果任一操作数的对应实类型为float,则将另一个操作数转换为对应实类型为 的类型,而不改变类型域float62)
62)例如,adouble _Complex和 a 的加法float只需 float要将操作数转换为double(并产生double _Complex结果)。

因此,基本上,如果您有一个具有两种不同类型的表达式,则具有较窄/不太精确类型的操作数将被提升为具有较宽/更精确类型的操作数的类型。

于 2012-11-16T22:21:15.177 回答
2

是的,C 有不同的版本,就像大多数软件产品有不同的版本一样。

K&R 是 Brian K ernighanDennis R itchie 在他们的书The C Programming Language中描述的原始版本。

第一个标准化版本是 ANSI C 或 C89,此后又出现了几个新版本。“当前 C”可以表示 C11(最新版本)或 C99(可能是当今最常用的版本)。

于 2012-11-16T22:02:25.910 回答
1

是的,您可以在评估时依靠 C 将浮点数提升为双精度数。

708 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. 

我正在使用此处找到的文档。

对不起,我之前引用了错误的东西

于 2012-11-16T22:04:27.080 回答