Question

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;
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top