float 除以 int 总是得到 float 吗? C++
Does dividing float by int always give float? C++
我在 C 和 C++ 上编程有一段时间了,并且始终确定双除 int 会得到 double,或者 double dividing int 也会得到 double(只有 int 被 int 得到 int),加法也是如此。我在做作业时确信 1 + (f*f + 10) / k
,其中 f
是 double
而 k
是 int
总是 return 加倍。
我在 mac 上使用 g++ -std=gnu++11
命令(所以它可能是 clang 编译器)并且我通过了测试(结果我确实得到了浮点数),但我的老师说它不确定它会浮动(他正在使用 windows)。该行为平台是否特定?是否有任何 C++ 标准描述 double on int 除法?谢谢!
我的代码:
#include <iostream>
using namespace std;
int main() {
int N;
double f, P = 0;
cin >> N >> f;
for (double k = 1; k <= N; k++){
P += 1 + (f*f + 10) / k;
}
cout << P;
return 0;
}
如果一个表达式包含两个参数,一个是 double
,另一个是 int
,那么该表达式的类型总是 double.
如果一个表达式包含两个参数,一个是 float
,另一个是 int
,那么该表达式的类型总是 float.
在许多方面,第二个陈述变得不合时宜(因为 64 位 int
可能指日可待)。请注意,在您的代码段中,您使用的是 double
而不是 float
.
一些模板元编程代码 (!) 供您展示给您的老师:
#include <iostream>
#include <type_traits>
int main(){
// Will output 1 if the types are the same, 0 otherwise
std::cout << std::is_same<decltype(int{} + double{}), double>::value;
}
您可以在空闲时将 double
和 int
替换为您自己选择的类型:代码所做的是将 int{} + double{}
的类型与 double
进行比较.
如你所见here:
[...] if either operand is double, the other operand is converted to double
[...] if either operand is float, the other operand is converted to float
所以标准保证该操作会给你一个 float
来自C++20标准草案[expr.arith.conv]:
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
- (1.1)
If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
- (1.2)
If either operand is of type long double, the other shall be converted to long double.
- (1.3)
Otherwise, if either operand is double, the other shall be converted to double.
- (1.4)
Otherwise, if either operand is float, the other shall be converted to float.
- (1.5)
Otherwise, the integral promotions ([conv.prom]) shall be performed on both operands.56 Then the following rules shall be applied to the promoted operands:
...
自至少 C++11(这是我检查过的最旧的)以来,该段落没有任何基本的变化,因此您的老师需要做一些解释。
我在 C 和 C++ 上编程有一段时间了,并且始终确定双除 int 会得到 double,或者 double dividing int 也会得到 double(只有 int 被 int 得到 int),加法也是如此。我在做作业时确信 1 + (f*f + 10) / k
,其中 f
是 double
而 k
是 int
总是 return 加倍。
我在 mac 上使用 g++ -std=gnu++11
命令(所以它可能是 clang 编译器)并且我通过了测试(结果我确实得到了浮点数),但我的老师说它不确定它会浮动(他正在使用 windows)。该行为平台是否特定?是否有任何 C++ 标准描述 double on int 除法?谢谢!
我的代码:
#include <iostream>
using namespace std;
int main() {
int N;
double f, P = 0;
cin >> N >> f;
for (double k = 1; k <= N; k++){
P += 1 + (f*f + 10) / k;
}
cout << P;
return 0;
}
如果一个表达式包含两个参数,一个是 double
,另一个是 int
,那么该表达式的类型总是 double.
如果一个表达式包含两个参数,一个是 float
,另一个是 int
,那么该表达式的类型总是 float.
在许多方面,第二个陈述变得不合时宜(因为 64 位 int
可能指日可待)。请注意,在您的代码段中,您使用的是 double
而不是 float
.
一些模板元编程代码 (!) 供您展示给您的老师:
#include <iostream>
#include <type_traits>
int main(){
// Will output 1 if the types are the same, 0 otherwise
std::cout << std::is_same<decltype(int{} + double{}), double>::value;
}
您可以在空闲时将 double
和 int
替换为您自己选择的类型:代码所做的是将 int{} + double{}
的类型与 double
进行比较.
如你所见here:
[...] if either operand is double, the other operand is converted to double
[...] if either operand is float, the other operand is converted to float
所以标准保证该操作会给你一个 float
来自C++20标准草案[expr.arith.conv]:
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
- (1.1) If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
- (1.2) If either operand is of type long double, the other shall be converted to long double.
- (1.3) Otherwise, if either operand is double, the other shall be converted to double.
- (1.4) Otherwise, if either operand is float, the other shall be converted to float.
- (1.5) Otherwise, the integral promotions ([conv.prom]) shall be performed on both operands.56 Then the following rules shall be applied to the promoted operands: ...
自至少 C++11(这是我检查过的最旧的)以来,该段落没有任何基本的变化,因此您的老师需要做一些解释。