C ++,在for()函数内将复数与普通双精度组合时出错
C++, Error when combining complex numbers with normal doubles inside a for() function
如果这个问题很明显,我很抱歉,但我无法弄清楚我的代码有什么问题。下面的解释,这里是代码:
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;
int main(){
//Variables:
complex<double> c; //Coordinate
complex<double> zn;
complex<double> znp1;
double iteration; //Iteration counter
double limit = 1.0e3; //zn must exceed limit
double resolution = 50.0; //Points/pixels per axis
double realrangeplus =5.0; //Coordinate system
double realrangeminus = -5.0;
double realsubdiv = (realrangeplus - realrangeminus)/resolution;
double imagrangeplus = 5.0;
double imagrangeminus = -5.0;
double imagsubdiv = (imagrangeplus - imagrangeminus)/resolution;
//cycle through real axis with c
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
//cycle through imaginary axis with c
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
//==========================================
iteration = 0.0; //reset
zn = (0.0, 0.0);
znp1 = (0.0, 0.0);
//Start iterating:
do{
iteration = iteration + 1.0; //count iterations
zn = znp1;
znp1 = zn*zn + c;
}while(abs(zn)<=limit);
cout << real(c) << " " << imag(c) << " " << real(zn) << " " << imag(zn) << " " << iteration << endl;
//==========================================
}
}
return 0;
}
这是在 Ubuntu 终端或 eclipse 中使用 g++ mandelbrot_skript.cpp
编译它时的错误:
mandelbrot_skript.cpp: In function ‘int main()’:
mandelbrot_skript.cpp:32:17: error: lvalue required as left operand of assignment
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
^~~~~~~~~~~~~~
mandelbrot_skript.cpp:32:79: error: lvalue required as left operand of assignment
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
^~~~~~~~~~
mandelbrot_skript.cpp:35:19: error: lvalue required as left operand of assignment
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
^~~~~~~~~~~~~~
mandelbrot_skript.cpp:35:81: error: lvalue required as left operand of assignment
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
^~~~~~~~~~
我正在尝试创建 mandelbrot 集的图像,代码可能完全错误,但这不是重点。
我不明白为什么会出现错误信息。据我所知,没有拼写错误,所有变量都具有正确的数据类型(我认为)并且我已经成功地尝试将实部和虚部与普通双精度变量相加和相乘。只有当我在 for()
函数中执行此操作时才会出现此错误。奇怪的是,完全相同的代码在名为 "CPP N-IDE".
的 android 应用程序中执行时没有问题
基本上,我做错了什么?
real(c)
是一个 reader 函数。 c.real(value)
是写函数。 source
因此:
for (c.real(realrangeminus); real(c) <= realrangeplus; c.real(real(c) + realsubdiv)) {
等等
For any pointer to an element of an array of complex named p and
any valid array index i, reinterpret_cast(p)[2*i] is the real part
of the complex number p[i], and reinterpret_cast(p)[2*i + 1] is
the imaginary part of the complex number p[i]
因此,如前所述,real(c)
和 imag(c)
将是 reader 读取变量的方法,而不是设置变量的方法。
可以找到有关复数使用的更多信息on Whosebug here
如果这个问题很明显,我很抱歉,但我无法弄清楚我的代码有什么问题。下面的解释,这里是代码:
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;
int main(){
//Variables:
complex<double> c; //Coordinate
complex<double> zn;
complex<double> znp1;
double iteration; //Iteration counter
double limit = 1.0e3; //zn must exceed limit
double resolution = 50.0; //Points/pixels per axis
double realrangeplus =5.0; //Coordinate system
double realrangeminus = -5.0;
double realsubdiv = (realrangeplus - realrangeminus)/resolution;
double imagrangeplus = 5.0;
double imagrangeminus = -5.0;
double imagsubdiv = (imagrangeplus - imagrangeminus)/resolution;
//cycle through real axis with c
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
//cycle through imaginary axis with c
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
//==========================================
iteration = 0.0; //reset
zn = (0.0, 0.0);
znp1 = (0.0, 0.0);
//Start iterating:
do{
iteration = iteration + 1.0; //count iterations
zn = znp1;
znp1 = zn*zn + c;
}while(abs(zn)<=limit);
cout << real(c) << " " << imag(c) << " " << real(zn) << " " << imag(zn) << " " << iteration << endl;
//==========================================
}
}
return 0;
}
这是在 Ubuntu 终端或 eclipse 中使用 g++ mandelbrot_skript.cpp
编译它时的错误:
mandelbrot_skript.cpp: In function ‘int main()’:
mandelbrot_skript.cpp:32:17: error: lvalue required as left operand of assignment
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
^~~~~~~~~~~~~~
mandelbrot_skript.cpp:32:79: error: lvalue required as left operand of assignment
for(real(c) = realrangeminus; real(c) <= realrangeplus; real(c) = real(c) + realsubdiv){
^~~~~~~~~~
mandelbrot_skript.cpp:35:19: error: lvalue required as left operand of assignment
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
^~~~~~~~~~~~~~
mandelbrot_skript.cpp:35:81: error: lvalue required as left operand of assignment
for(imag(c) = imagrangeminus; imag(c) <= imagrangeplus; imag(c) = imag(c) + imagsubdiv){
^~~~~~~~~~
我正在尝试创建 mandelbrot 集的图像,代码可能完全错误,但这不是重点。
我不明白为什么会出现错误信息。据我所知,没有拼写错误,所有变量都具有正确的数据类型(我认为)并且我已经成功地尝试将实部和虚部与普通双精度变量相加和相乘。只有当我在 for()
函数中执行此操作时才会出现此错误。奇怪的是,完全相同的代码在名为 "CPP N-IDE".
基本上,我做错了什么?
real(c)
是一个 reader 函数。 c.real(value)
是写函数。 source
因此:
for (c.real(realrangeminus); real(c) <= realrangeplus; c.real(real(c) + realsubdiv)) {
等等
For any pointer to an element of an array of complex named p and any valid array index i, reinterpret_cast(p)[2*i] is the real part of the complex number p[i], and reinterpret_cast(p)[2*i + 1] is the imaginary part of the complex number p[i]
因此,如前所述,real(c)
和 imag(c)
将是 reader 读取变量的方法,而不是设置变量的方法。
可以找到有关复数使用的更多信息on Whosebug here