3
我试图将此algorithm移植到clojure。Clojure/Java Mandelbrot分形绘图
我的代码是
(defn calc-iterations [x y]
(let [c (struct complex x y)]
(loop [z (struct complex 0 0)
iterations 0]
(if (and (< 2.0 (abs z))
(> max-iterations iterations))
iterations
(recur (add c (multiply z z)) (inc iterations))))))
乘,加和ABS功能的工作,因为他们应该。我用计算器测试了它们。然而,对于以下值:
(calc-iterations 0.60703135 -0.33984375) ; should give me 2, instead I get 4
(calc-iterations -1.8421874 0.3515625) ; should give me 1, instead I get 3
我检查使用,我在网上找到了另一个Java小程序正确的迭代次数。它似乎正在工作,因为它会产生正确的输出。它的迭代功能是
protected int calcIterations(float x, float y) {
int iterations = 0;
float xn = x, yn = y;
while (iterations < MAX_ITERATIONS) {
float xn1 = xn*xn - yn*yn;
float yn1 = 2*xn*yn;
xn = xn1 + x;
yn = yn1 + y;
float magsq = xn*xn + yn*yn;
if (magsq > 4)
break;
iterations++;
}
System.out.println(x + " " + y + " " + iterations);
return iterations;
}
任何人都可以发现我的错误吗?