乘以混合数据类型时,float总是自动转换为double吗? [英] Does float always auto-convert to double when multiplying mixed data types?
问题描述
在史蒂文·普拉塔(Steven Prata)的书"C Primer Plus"中,有一个关于类型转换的部分,其中基本规则"是类型转换".部分已在规则1中说明:
In Steven Prata's book "C Primer Plus", there's a section on Type Conversions, wherein "The basic rules are" section has stated in rule 1:
在K& R C下,但不在当前C下,float会自动转换为double.
Under K&R C, but not under current C, float is automatically converted to double.
http://www.9wy.net/onlinebook/CPrimerPlus5/ch05lev1sec5.html
有人可以解释的意思,但不能解释当前的C
的意思吗?有自动转换的C版本和没有自动转换的版本吗?
Could someone explain what but not under current C
means? Are there versions of C that auto-convert and versions that don't?
我试图理解是否有一个混合了浮点数和双精度数的表达式,我可以依靠C来将浮点数提高为双精度吗?
I'm trying understand if I have an expression that mixes floats and doubles, can I rely on C to promote floats to doubles when it's evaluated?
推荐答案
它必须引用 float * float
格式的二进制算术运算的结果.在C语言的标准版中,此类表达式的操作数被提升为 double
,结果具有 double
类型.
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.
例如,这是《 C参考手册》的引用
For example, here's a quote from "C Reference Manual"
如果两个操作数均为 int 或 char ,则结果为 int .如果两者都是 float 或 double ,结果为 double .
If both operands are int or char, the result is int. If both are float or double, the result is double.
在C89/90中,此行为已被更改,并且 float * float
表达式产生了 float
结果.
In C89/90 already this behavior was changed and float * float
expressions produce float
result.
- 如果其中一个操作数的类型为 long double ,则另一个操作数将转换为 long double
- 否则,如果其中一个操作数为 double ,则另一个操作数将转换为 double .
- 否则,如果其中一个操作数为 float ,则另一个操作数将转换为 float .
- 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.
这篇关于乘以混合数据类型时,float总是自动转换为double吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!