I have been working on a function which creates a 1 million digit value of pi but php is treating a theoretical numeric variable as a string. Below is the code I used and for now I have put a small limit on the digits but it is line 22 that wont calculate. Does anybody know how to make it so that a function inside a variable can be executed with its mathematical calculations?

<?
function realpi()
    {
    $pival=1;
    while ($pirow<6)
        {
        $pirow+=1;
        $pisubrow=1;
        $tempval="(0.5*sqrt(2";
        while ($pisubrow<$pirow)
            {
            $tempval.="+sqrt(2";
            $pisubrow+=1;
            }
        $pisubrow=1;
        while ($pisubrow<$pirow)
            {
            $tempval.=")";
            $pisubrow+=1;
            }
        $tempval.="))";
        $pival=$pival*$tempval;
        var_dump($tempval);
        echo "<br>";
        }
    $pival=$pival/2;
    return $pival;
    unset($pirow);
    }
echo realpi();
?>

With the echo functions inside the code, the below is echoed

string(13) "(0.5*sqrt(2))" 
string(21) "(0.5*sqrt(2+sqrt(2)))" 
string(29) "(0.5*sqrt(2+sqrt(2+sqrt(2))))" 
string(37) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))" 
string(45) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2))))))" 
string(53) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))))" 
0

All except the last line in the above box is a var_dump of the looped variable which will not perform the mathematical and function calculations. The last line is the answer that the function has got for pi. The reason why it is zero is because of line 22 where $tempval is a string. But it needs to be a mathematical string as it has functions that need to execute. Does anyone know how to execute this variable?

I have just discovered a major bug in php and an example is the below code. For some reason php rounds the variable $var to the number 2 and displays "2" instead of 1.99999999999999999999999999999999999999. So how would I make php display 1.99999999999999999999999999999999999999 while keeping the variable an integre for mathematical calculations as I need to display a 1 million digit number?

$var=1.99999999999999999999999999999999999999;
echo $var;

//displays "2" without the quotes

one way you can execute it is by making a dynamic function using create_function(). there might be a better way though. I can't think of any others.

I have just checked the create_function() element/function in the official documentation but I can't see how impliment it into my code. However, I have made a second piece of that that might calculate pi (haven't confirmed this new method follows the algorithom) which should solve my first problem leaving my digit problem. The code I currently have is as follows:

<?
function realpi()
    {
    $pival=1;
    while ($pirow<25)
        {
        $pirow+=1;
        $pisubrow=0;
        $tempval="0";
        while ($pisubrow<$pirow)
            {
            $tempval=sqrt(2+$tempval);
            $pisubrow+=1;
            }
        $pival=0.5*$tempval*$pival;
        var_dump($tempval);
        echo "<br>";
        }
    $pival=$pival*2;
    return $pival;
    unset($pirow);
    }

echo realpi();
?>

But the only problem is that php is rounding it to the eleventh digit. Is there any way around it because I need 1 million digits.

in the first post it looked like you were creating the calculation as a string that needed to be executed. the create_function() function will let you add that code to a function and return the result you needed when ran. you changed it in the last post.

I have just discovered a major bug in php and an example is the below code. For some reason php rounds the variable $var to the number 2 and displays "2" instead of 1.99999999999999999999999999999999999999. So how would I make php display 1.99999999999999999999999999999999999999 while keeping the variable an integre for mathematical calculations as I need to display a 1 million digit number?

$var=1.99999999999999999999999999999999999999;
echo $var;

//displays "2" without the quotes

This isn't a bug. Floats in PHP will have a limited precision (since they have a set storage size), so rounding off occurs when you go over.
http://www.php.net/float

You might want to look into using the math extensions:
http://www.php.net/manual/en/refs.math.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.