我已经包含了一个演示我的问题的片段。基本上处理给了我这个错误:
map(NaN, -3, 3, -125, 125) called, which returns NaN (not a number)
我理解此消息的方式是,map 函数返回 NaN,并且由于它返回一个浮点数,我应该能够使用 Float.NaN 进行检查。正如所演示的,尽管在创建检查它的 if 语句时我没有得到任何结果。我尝试将 if 语句放在带有相应变量的 map 函数之前,但没有命中。我想知道是否有人可以向我解释这种现象并帮助我调试代码。这可能是我正在监督的一些小事情,但它让我发疯。提前致谢
片段:
void setup() {
size(500, 500);
}
void draw() {
background(0);
}
class Complex {
private float re, im, r, p;
Complex(float real, float imag) {
re = real;
im = imag;
r = sqrt(sq(re)+sq(im)); //radius
p = atan2(im, re); //phase
}
Complex Div(Complex b) {
Complex a = this;
float real = (a.re*b.re+a.im*b.im)/(sq(b.re)+sq(b.im));
float imag = (a.im*b.re-a.re*b.im)/(sq(b.re)+sq(b.im));
return new Complex(real, imag);
}
Complex Ln() {
float real = log(r);
float imag = p;
return new Complex(real, imag);
}
Complex LogBase(Complex b) {
Complex a = this;
return a.Ln().Div(b.Ln());
}
Complex Scale(float scale, int dim) {
float real = map(re, -scale, scale, -dim, dim);
float imag = map(im, -scale, scale, -dim, dim);
if (real == Float.NaN || imag == Float.NaN) {
print("\nHit!");
}
return new Complex(real, imag);
}
}
void keyPressed() {
float d = width/4;
for (float z = -d; z<d; z++) {
for (float x = -d; x<d; x++) {
Complex c = new Complex(1, 5);
c = c.LogBase(new Complex(x, z));
c.Scale(3.0, int(d));
}
}
}
翻阅古今
相关分类