number string

Reply

Join Date: Sep 2007
Posts: 1,440
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

number string

 
0
  #1
Nov 30th, 2008
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?
  1. <?
  2. function realpi()
  3. {
  4. $pival=1;
  5. while ($pirow<6)
  6. {
  7. $pirow+=1;
  8. $pisubrow=1;
  9. $tempval="(0.5*sqrt(2";
  10. while ($pisubrow<$pirow)
  11. {
  12. $tempval.="+sqrt(2";
  13. $pisubrow+=1;
  14. }
  15. $pisubrow=1;
  16. while ($pisubrow<$pirow)
  17. {
  18. $tempval.=")";
  19. $pisubrow+=1;
  20. }
  21. $tempval.="))";
  22. $pival=$pival*$tempval;
  23. var_dump($tempval);
  24. echo "<br>";
  25. }
  26. $pival=$pival/2;
  27. return $pival;
  28. unset($pirow);
  29. }
  30. echo realpi();
  31. ?>
With the echo functions inside the code, the below is echoed
  1. string(13) "(0.5*sqrt(2))"
  2. string(21) "(0.5*sqrt(2+sqrt(2)))"
  3. string(29) "(0.5*sqrt(2+sqrt(2+sqrt(2))))"
  4. string(37) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))"
  5. string(45) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2))))))"
  6. string(53) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))))"
  7. 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?
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,440
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: number string

 
0
  #2
Nov 30th, 2008
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?
  1. $var=1.99999999999999999999999999999999999999;
  2. echo $var;
  3.  
  4. //displays "2" without the quotes
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: number string

 
0
  #3
Nov 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,440
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: number string

 
0
  #4
Nov 30th, 2008
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:
  1. <?
  2. function realpi()
  3. {
  4. $pival=1;
  5. while ($pirow<25)
  6. {
  7. $pirow+=1;
  8. $pisubrow=0;
  9. $tempval="0";
  10. while ($pisubrow<$pirow)
  11. {
  12. $tempval=sqrt(2+$tempval);
  13. $pisubrow+=1;
  14. }
  15. $pival=0.5*$tempval*$pival;
  16. var_dump($tempval);
  17. echo "<br>";
  18. }
  19. $pival=$pival*2;
  20. return $pival;
  21. unset($pirow);
  22. }
  23.  
  24. echo realpi();
  25. ?>
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.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: number string

 
0
  #5
Nov 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: number string

 
0
  #6
Dec 8th, 2008
Originally Posted by cwarn23 View 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?
  1. $var=1.99999999999999999999999999999999999999;
  2. echo $var;
  3.  
  4. //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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC