将整数类型转换为 double 以从类型转换整数的除法中获得 double 结果
Typecasting integers as doubles to get a double result from divison of the typecasted integers
我不明白以下示例之间输出为双精度的根本原因。在以下方面:
- 为什么 double 除以 int 会得到 double?
为什么 int 除以 double 结果是 double?
#include <stdio.h>
int main(int agrc, char **argv)
{
double d;
int a=5,b=2;
d = (double)a/b;
printf("d= %G\n",d); // outputs 2.5
d = a/(double)b;
printf("d= %G\n",d); // outputs 2.5
}
转换优先于除法,double
和 int
之间的运算将产生 double
来自C standard, section 6.3.1.8: 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.
因此,如果算术运算符的一个操作数是 int
,另一个是 double
,则标准规定结果表达式的类型为 double
.
由于计算的需要,如果一个除法的操作数之一是double数据类型,另一个将自动提升为计算方式。在您的示例中,发生这种情况是因为显式强制转换的运算符优先级高于除法。
如果只想转换除法的结果,可以这样做:
d = (double)(a/b);
确保先执行整数除法,然后再执行显式转换为 double。
对于@dbush 的出色回答的其他上下文,重要的是要注意标准指定对于包含不同类型的所有算术类型转换,该转换是从两种类型中较小的类型到最大的类型:
总结 from C11 - 6.3.1.8 常用算术转换:
1st: if real type of either operand is long double, the other is
converted to long double
2nd: Otherwise if real type of either
operand is double, the other is converted to double
3rd: Otherwise
if real type of either operand is float, the other is converted to
float
它继续指定如何以类似的方式进行整数促销...
我不明白以下示例之间输出为双精度的根本原因。在以下方面:
- 为什么 double 除以 int 会得到 double?
为什么 int 除以 double 结果是 double?
#include <stdio.h> int main(int agrc, char **argv) { double d; int a=5,b=2; d = (double)a/b; printf("d= %G\n",d); // outputs 2.5 d = a/(double)b; printf("d= %G\n",d); // outputs 2.5 }
转换优先于除法,double
和 int
之间的运算将产生 double
来自C standard, section 6.3.1.8: 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.
因此,如果算术运算符的一个操作数是 int
,另一个是 double
,则标准规定结果表达式的类型为 double
.
由于计算的需要,如果一个除法的操作数之一是double数据类型,另一个将自动提升为计算方式。在您的示例中,发生这种情况是因为显式强制转换的运算符优先级高于除法。
如果只想转换除法的结果,可以这样做:
d = (double)(a/b);
确保先执行整数除法,然后再执行显式转换为 double。
对于@dbush 的出色回答的其他上下文,重要的是要注意标准指定对于包含不同类型的所有算术类型转换,该转换是从两种类型中较小的类型到最大的类型:
总结 from C11 - 6.3.1.8 常用算术转换:
1st: if real type of either operand is long double, the other is converted to long double
2nd: Otherwise if real type of either operand is double, the other is converted to double
3rd: Otherwise if real type of either operand is float, the other is converted to float
它继续指定如何以类似的方式进行整数促销...