diff --git a/mandelbrot.cpp b/mandelbrot.cpp index 3bb097d..a7ebe82 100644 --- a/mandelbrot.cpp +++ b/mandelbrot.cpp @@ -18,22 +18,25 @@ along with this program. If not, see . */ #include "mandelbrot.h" +#include "mpreal.h" #include "utils.h" #include #include +#include using namespace mpfr; Mandelbrotc::Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi) : f(f), t(t), s(s), max_iter(mi) { + mpreal::set_default_prec(digits2bits(100)); this->threads = std::vector(); - //this->screen = std::vector>(); - this->screen = new uint8_t*[s.x]; + this->screen = new double*[s.x]; for(int i = 0; i < s.x; ++i) - this->screen[i] = new uint8_t[s.y]; + this->screen[i] = new double[s.y]; this->done = false; + this->diff = mpreal(0.0001); } void Mandelbrotc::start_threads(bool first) @@ -70,14 +73,16 @@ void Mandelbrotc::calc(const uint8_t tid) } } + this->updatePrec(); + //std::cout << "thread " << tid << " done" << std::endl; if (tid == thread_count-1) this->done = true; } -uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n) +double Mandelbrotc::mandelbrot(const Vec2mp &n) { - uint32_t iter = 0; + double iter = 0.0; // BigFloat x = BigFloat(n.x.precision); // x.setValue(0.0); @@ -88,15 +93,65 @@ uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n) mpreal y = mpreal(0.0); mpreal x2 = mpreal(0.0); mpreal y2 = mpreal(0.0); + mpreal xtemp = mpreal(0.0); - while (x2 + y2 <= 4 && iter < max_iter) { - y = 2.0 * x * y + n.y; - x = x2 - y2 + n.x; - x2 = x * x; - y2 = y * y; + // while (x2 + y2 <= (1 << 16) && iter < max_iter) { + // y = 2.0 * x * y + n.y; + // x = x2 - y2 + n.x; + // x2 = x * x; + // y2 = y * y; + + // iter++; + // } + + while (x*x + y*y <= (1 << 16) && iter < max_iter) { + xtemp = x*x - y*y + n.x; + y = 2*x*y + n.y; + x = xtemp; iter++; } - return iter;//+ 1 - log(log2(abs((int)x2))); + if (iter == max_iter) + return max_iter; + + double log_zn = (mpfr::log(x*x + y*y) / 2).toDouble(); + double nu = log(log_zn / log(2)) / log(2); + + iter = iter + 1 - nu; + + return iter; + //return iter + 1 - log(log2(x2.toDouble()+y2.toDouble())); +} + +void Mandelbrotc::updatePrec() +{ + //std::cout << "before updatePrec: " << this->f.x.get_prec() << std::endl; + mpfr_prec_t bprec = this->f.x.get_prec(); + mpfr_prec_t aprec; + + std::cout << "before updatePrec def: " << mpfr::bits2digits( + bprec) << std::endl; + + if (mpfr::abs(mpfr::abs(this->f.x) - mpfr::abs(this->t.x)) < diff || + (mpfr::abs(this->f.x) - mpfr::abs(this->t.y)) < diff) { + + aprec = mpfr::digits2bits(mpfr::bits2digits(bprec)+5); + + std::cout << "true" << std::endl; + } + else { + return; + } + + this->f.x.set_prec(aprec); + this->f.y.set_prec(aprec); + this->t.x.set_prec(aprec); + this->t.y.set_prec(aprec); + + //std::cout << "after updatePrec: " << this->f.x.get_prec() << std::endl; + std::cout << "after updatePrec def: " << mpfr::bits2digits( + this->f.x.get_prec()) << std::endl; + + diff = mpfr::abs(mpfr::abs(this->f.x) - mpfr::abs(this->t.x)); } diff --git a/mandelbrot.h b/mandelbrot.h index 3b0312c..2f34e58 100644 --- a/mandelbrot.h +++ b/mandelbrot.h @@ -32,16 +32,18 @@ class Mandelbrotc { public: Vec2mp f, t; uint8_t thread_count; + mpfr::mpreal diff; std::vector threads; Vec2i s; //std::vector> screen; - uint8_t **screen; + double **screen; bool done; uint32_t max_iter; Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi); void start_threads(bool first); void stop_threads(); - uint32_t mandelbrot(const Vec2mp &n); + double mandelbrot(const Vec2mp &n); void calc(const uint8_t tid); + void updatePrec(); };