C++ 如何将整数隐式转换为双精度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37777053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert integer to double implicitly?
提问by Milo Lu
int a{5},b{2},c{9};
double d = (double)a / (double)b + (double)c;
Or I can use static_cast
. Either way is verbose, especially when the formula is long. Is there a better solution?
或者我可以使用static_cast
. 无论哪种方式都是冗长的,尤其是当公式很长时。有更好的解决方案吗?
回答by oklas
You can multiply by 1.0:
您可以乘以 1.0:
int a{5}, b{2}, c{9};
double d = 1.0 * a / b + 1.0 * c;
And when you work with sums you can add to 0.0:
当您使用总和时,您可以添加到 0.0:
double d = 0.0 + a - b + c;
Most compilers perform optimization such that the meaningless operation is not evaluated. Only type conversion is done.
大多数编译器执行优化,以便不评估无意义的操作。只进行类型转换。
Remember that you only need to cast the first member in each division/multiply group. Do so in any manner that seems reasonable. And simple addition/substraction (with no other type multipliers/divisors) is casted too. Compilers guarantee casting. So your example:
请记住,您只需要在每个除法/乘法组中投射第一个成员。以任何看似合理的方式进行。并且也投射了简单的加法/减法(没有其他类型的乘数/除数)。编译器保证铸造。所以你的例子:
double d = (double)a / (double)b + (double)c;
Really may be rewritten like this:
真的可以改写成这样:
double d = (double)a / b + c;
double d = 1.0 * a / b + c;
double d = static_cast<double>(a) / b + c;
Some more examples:
还有一些例子:
double d = (double)a / b + (double)c / d + e;
double d = 1.0 * a / b + 1.0 * c / d + e;
double d = static_cast<double>(a) / b + static_cast<double>(c) / d + e;
回答by Richard Hodges
Is there a better solution?
有更好的解决方案吗?
Yes. Express intent through functions.
是的。通过功能表达意图。
Marvel as the optimiser emits perfectly efficient assembler. Enjoy the accolades of your colleagues who gaze in wonder at your awesomely readable and maintainable code:
惊叹于优化器发出完美高效的汇编器。享受您的同事的赞誉,他们惊叹于您的可读性和可维护性极好的代码:
#include <iostream>
auto a_over_b_plus_c(double a, double b, double c)
{
double d = a / b + c;
return d;
}
int main()
{
int a = 5, b = 2, c = 9;
std::cout << a_over_b_plus_c(a, b, c) << std::endl;
}
For fun, here's a solution based on tuples & lambdas:
为了好玩,这里有一个基于元组和 lambda 的解决方案:
#include <iostream>
#include <tuple>
template<class T, class...Args>
auto to(Args&&...args)
{
return std::make_tuple(T(std::forward<Args>(args))...);
}
int main()
{
int a = 5, b = 2, c = 9;
auto calc = [](auto&& vals) {
auto& a = std::get<0>(vals);
auto& b = std::get<1>(vals);
auto& c = std::get<2>(vals);
return a / b + c;
};
auto result = calc(to<double>(a, b, c));
std::cout << result << std::endl;
}
... and something perhaps more readable...
......还有一些可能更具可读性的东西......
#include <iostream>
#include <tuple>
#include <complex>
template<class T, class F, class...Args>
auto with(F f, Args&&...args)
{
return f(T(std::forward<Args>(args))...);
}
int main()
{
int a = 5, b = 2, c = 9;
auto calc = [](auto&& a, auto&& b, auto&& c) {
return a / b + c;
};
auto result = with<double>(calc, a, b, c);
auto result2 = with<float>(calc, a, b, c);
auto result3 = with<std::complex<double>>(calc, a, b, c);
auto result4 = with<std::complex<float>>(calc, a, b, c);
std::cout << result << std::endl;
std::cout << result2 << std::endl;
std::cout << result3 << std::endl;
}
回答by doug
This works but all you need is a single 1.0*
in front of a
这有效,但您需要的只是1.0*
前面的一个a
int a{5},b{2},c{9};
double d = (double)a / (double)b + (double)c;
int a{5},b{2},c{9};
double d = 1.0*a / b + c;
The rules of precedence and implicit conversion will cause all the variables to be converted to doubles.
优先级和隐式转换规则将导致所有变量都转换为双精度型。
One thing to be careful of is grouped variables which will need to have their own 1.0*
or 0.0+
as appropriate:
需要注意的一件事是分组变量,这些变量需要有自己的1.0*
或0.0+
适当的:
int a{5},b{2},c{9};
double d = a / (0.0 + b + c);
int a{5},b{2},c{9};
double d = a / (1.0 * b * c);
Alternately, one use use a static cast on the associated variable. I prefer the smaller version as the 1.0*
or 0.0+
both scream out implicit conversion to doubles.
或者,一种使用对关联变量使用静态转换。我喜欢较小的版本作为1.0*
或0.0+
两者尖叫了隐式转换双打。
int a{5},b{2},c{9};
double d = a / (static_cast<double>(b) * c);