将 int 转换为双精度以执行 std::complex <double>的"*"操作
convert int to double for `*` operations with std::complex<double>
我是一名物理学家,试图尽量减少在用于计算的代码文件中键入类型转换和强制转换(方程和函数)。计算通常涉及复数。因此,我扩展了complex<double>
类型,cd
紧凑性并添加了一些帮助方法。
class cd: public complex<double> { ... }
扩展而不是仅仅使用typedef
的原因是,物理符号(string
)和物理单位(string
)可以与物理变量一起存储。
现在,如果在计算中我有这样的实例
int i = 2;
cd z(1,2);
cout << i*z;
这会产生错误,因为没有运算符将int
相乘并cd
。(tbh 我认为 c++ 会自动将int
隐式转换为double
并使用相关运算符。在手动定义这样的运算符时
cd operator*(const int& i, const cd& z)
{
return cd(i*z.real(),i*z.imag());
}
然后,C++警告诸如此类部分的类型转换的歧义
double x = 30;
x*z;
在下面的x
是双精度,I
是cd
。
error: ambiguous overload for ‘operator*’ (operand types are ‘double’ and ‘const cd’)
return pow(eps1/e0*kz2,2)-pow(eps2/e0*kz1*tanh(dist*1e-10*kz1/( x *I)),2);
~~~^~
In file included from libs/calc/include/calculation.h:12:0,
from scripts/dist_dependence.cpp:2:
libs/calc/include/complex_double.h:49:4: note: candidate: cd operator*(const int&, const cd&)
cd operator*(const int& x, const cd& z)
由于手动操作员定义(如上)也可用于具有cd
的double
- 这已经在标准库中定义。
现在可以通过定义来解决上述问题
cd operator*(const double& x, const cd& z)
{
return cd(x*z.real(),x*z.imag());
}
但是,这可以防止以下情况:
除此之外,我还希望从cd
转换为double
,以便可以将复数传递(无需显式转换)到接受实数(double
类型)参数的函数。(如果虚部为零,则cd
转换为double
,否则抛出错误或其他东西)。 问题是当我定义(除了double
-cd
运算符*
operator double() {
if (imag()==0.0) return real();
throw "trying to cast a cd with non-zero imaginary part to double";
}
cd
类内部。
它吐出以下内容:
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
例如,这里只给出了operator*
,但我也想为其他数学二进制运算执行此操作。
你的问题在这里:
所以我扩展了
complex<double>
类型作为cd
这是初学者常犯的错误,认为继承是所有问题的答案,而实际上它是许多问题的根源。
只需定义一个没有继承的工作类型,一切都会开箱即用:
using cd = std::complex<double>;
constexpr cd i{0 , 1};
int main (int , char **)
{
cd x{ 1, 3};
std::cout << x << 'n';
std::cout << x*i << 'n';
std::cout << x*i + 3.2 << 'n';
return 0;
}
https://wandbox.org/permlink/OfOfonJFrTInR0ib
免责声明:cd
不是此符号的最佳名称。想想一些更具描述性的
我创建了一个最小的例子,我相信它展示了你的问题。希望它也能向评论者说明您要完成的任务。
#include <iostream>
#include <complex>
#include <string>
class cd: public std::complex<double> {
public:
cd(double re, double im):std::complex<double>(re,im),name("var1"){}
operator double(){
if (imag()==0.0) return real();
throw "trying to cast a cd with non-zero imaginary part to double";
}
friend std::ostream& operator<<(std::ostream& os, const cd& z){
os << z.name << "=(" << z.real() << "," << z.imag() << ")";
return os;
}
private:
std::string name;
};
cd operator*(const int& i, const cd& z){
return cd(i*z.real(),i*z.imag());
}
cd operator*(const double& x, const cd& z){
return cd(x*z.real(),x*z.imag());
}
void foo(double x){
std::cout << "foo " << x << std::endl;
}
int main(){
int i=2;
cd z(1,2);
std::cout << i*z << std::endl;
double x=30;
std::cout << x*z << std::endl;
cd zz(3,0);
foo(x*zz);
std::cout << z*zz << std::endl;
}
它给出了以下来自g++
(版本 7.4.0)的输出
test_complex_double.cc: In function ‘int main()’:
test_complex_double.cc:48:18: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
std::cout << z*zz << std::endl;
^~
In file included from test_complex_double.cc:2:0:
/usr/include/c++/7/complex:386:5: note: candidate 1: std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&) [with _Tp = double]
operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
^~~~~~~~
test_complex_double.cc:22:4: note: candidate 2: cd operator*(const int&, const cd&)
cd operator*(const int& i, const cd& z){
^~~~~~~~
test_complex_double.cc:48:18: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
std::cout << z*zz << std::endl;
^~
In file included from test_complex_double.cc:2:0:
/usr/include/c++/7/complex:386:5: note: candidate 1: std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&) [with _Tp = double]
operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
^~~~~~~~
test_complex_double.cc:26:4: note: candidate 2: cd operator*(const double&, const cd&)
cd operator*(const double& x, const cd& z){
^~~~~~~~
这只是一个警告,此示例仍在编译。
我认为解决方案是您希望您的类成为std::complex<double>
的容器,而不是从中继承。我假设你想要继承,这样你就不必围绕std::complex<double>
实现的所有内容实现包装器函数,但在我看来,容器方法更有意义,并且还解决了这个特定问题。
下面是一个显示容器替代方案的工作示例:
#include <iostream>
#include <complex>
#include <string>
class cd {
public:
cd(double re, double im):val(re,im),name("var1"){}
cd(const std::complex<double>& v):val(v),name("var1"){}
operator double(){
if (val.imag()==0.0) return val.real();
throw "trying to cast a cd with non-zero imaginary part to double";
}
friend std::ostream& operator<<(std::ostream& os, const cd& z){
os << z.name << "=(" << z.real() << "," << z.imag() << ")";
return os;
}
double real() const{return val.real();}
double imag() const{return val.imag();}
cd operator*(const cd& other)const{return val*other.val;}
private:
std::complex<double> val;
std::string name;
};
cd operator*(const int& i, const cd& z){
return cd(i*z.real(),i*z.imag());
}
cd operator*(const double& x, const cd& z){
return cd(x*z.real(),x*z.imag());
}
void foo(double x){
std::cout << "foo " << x << std::endl;
}
int main(){
int i=2;
cd z(1,2);
std::cout << i*z << std::endl;
double x=30;
std::cout << x*z << std::endl;
cd zz(3,0);
foo(x*zz);
std::cout << z*zz << std::endl;
}
这将在没有警告的情况下编译,运行程序会产生输出:
var1=(2,4)
var1=(30,60)
foo 90
var1=(3,6)
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ