-2

In my function I want to use php bc-math to improve the precision. I've tried to replace all operations to no avail. Is this is a float-to-string conversion problem?

    function complex_iterate($re,$im)
   {
    $re=strval($re);
    $im=strval($im);
    $zisqr = $zrsqr = $iter = $zIm = $zRe = "0";
    bcscale(50);

   while (floatval(bcadd($zrsqr,$zisqr)) < 4
     && $iter < $this->iterations
    )
    {
        $zIm = bcadd($zIm,$zRe);
        $zIm = bcadd($zIm,$zIm);
        $zIm = bcadd($zIm,$im);
        $zRe = bcadd(bcsub($zrsqr,$zisqr),$re);
        $zrsqr = bcmul($zRe,$zRe);
        $zisqr = bcmul($zIm,$zIm);
        ++$iter;
    }
    return $iter;
Micromega
  • 12,486
  • 7
  • 35
  • 72
  • 1
    It's going to be crazy slow. I had Mandelbrot set iterations run very slowly even using C++'s std::complex class, doing it in an arbitrary precision library will likely be even slower. – robbrit Dec 17 '12 at 16:17
  • Yeah for sure using an arbitrary-precision library will help improve quality, at least when you go past the precision of floating-point numbers. It's just going to be slow. – robbrit Dec 17 '12 at 21:42
  • I don't really know anything about bc-math, however even without knowing anything I can see that you're using a `bcadd` on your first line when you should be using a `bcmul`. – robbrit Dec 17 '12 at 21:47

1 Answers1

1

Using any arbitrary-precision library will be much slower than floating-point numbers, especially for something like calculating the Mandelbrot set which does many, many repeated iterations. If you want speed I'd recommend rewriting this in C using a library like gmplib.

The issue in your code is that you are using a bcadd instead of a bcmul for your first line inside the loop.

robbrit
  • 17,560
  • 4
  • 48
  • 68