3

I have this algorithm that calcuates the mandelbrot value of a point (x0,y0) (x0 and y0 are somewhere between -1 and 1 i thought, not very important). This is all going very well when scale isn't getting too big, but at higher values of scale, the values returned are very inaccurate and my graphic output starts to go freaky. How do i predict from what value of scale this occurs?

    public static byte CalculateMandelbrot(double x0, double y0,double scale)
    {
        x0 /= scale;
        y0 /= scale;
        double y = 0;
        double x = 0;
        byte i = 0;
        while (x * x + y * y < 4)
        {
            double tx = x;
            x = x * x - y * y + x0;
            y = 2 * tx * y + y0;
            i++;
            if (i == 0xFF) break;
        }

        return i;
    }
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
JimmyMcHoover
  • 764
  • 7
  • 15
  • I don't know the maths, but since you only give it 0xFF iterations, maybe it isn't enough to converge. – Jake Sep 16 '12 at 19:24
  • @jake No the low amount of iterations just causes some shapes to be rounded a bit, also unwanted, but an entirely different problem. – JimmyMcHoover Sep 17 '12 at 11:27
  • 1
    You have 6 multiplications instead of 3. Decimal still is painfully slow, but you can almost double its speed by refactoring your code..:` do { x2 = x * x; y2 = y * y; y = x * y; y += y + y0; x = x2 - y2; x += x0; k++; } while (k < kMax && (x2 + y2) < limit);` – TaW Oct 17 '14 at 18:12

2 Answers2

4

A double has 53 bits of precision. This amounts to about 16 decimal places.

If you zoom in on your fractal 10^13 times, and make picture of 1000x1000 pixels, the precision is about the same as the screen resolution: the minimal change possible in a double is a step of one pixel on the screen.

But you will get into trouble before that, because you iterate the mandelbrot formula a hundred times iteratively on the same number. Each calculation adds a roundoff error (multiple ones, probably) of about 1/10^16. It is possible (although tedious) to predict when this gets noticable.

The FPU internally has a higher number of bits than standard double, this will reduce the abovementioned effect.

0

This is the classic "decimal vs double" pitfall. Try using 'decimal' for all vars and see if it clicks.

From the C# Reference page:

Compared to floating-point types, the decimal type has a greater precision and a smaller range

There are also arbitrary precision implementations like BigFloat Class.

Ohad
  • 2,752
  • 17
  • 15