我想編程的樂趣我自己的正弦函數實現,但我不斷收到:PHP「最大執行」
Fatal error: Maximum execution time of 30 seconds exceeded
我有一個小的HTML表單,您可以進入的「X」值Sin(x)您要查找的數量和您要計算的「迭代次數」(您的數值的精確度),其餘爲PhP。 的數學是基於正弦波的「系列定義」的維基百科: - >http://en.wikipedia.org/wiki/Sine#Series_definition 這裏是我的代碼:
<?php
function factorial($int) {
if($int<2)return 1;
for($f=2;$int-1>1;$f*=$int--);
return $f;
};
if(isset($_POST["x"]) && isset($_POST["iterations"])) {
$x = $_POST["x"];
$iterations = $_POST["iterations"];
}
else {
$error = "You forgot to enter the 'x' or the number of iterations you want.";
global $error;
}
if(isset($x) && is_numeric($x) && isset($iterations) && is_numeric($iterations)) {
$x = floatval($x);
$iterations = floatval($iterations);
for($i = 0; $i <= ($iterations-1); $i++) {
if($i%2 == 0) {
$operator = 1;
global $operator;
}
else {
$operator = -1;
global $operator;
}
}
for($k = 1; $k <= (($iterations-(1/2))*2); $k+2) {
$k = $k;
global $k;
}
function sinus($x, $iterations) {
if($x == 0 OR ($x%180) == 0) {
return 0;
}
else {
while($iterations != 0) {
$result = $result+(((pow($x, $k))/(factorial($k)))*$operator);
$iterations = $iterations-1;
return $result;
}
}
}
$result = sinus($x, $iterations);
global $result;
}
else if(!isset($x) OR !isset($iterations)) {
$error = "You forgot to enter the 'x' or the number of iterations you want.";
global $error;
}
else if(isset($x) && !is_numeric($x)&& isset($iterations) && is_numeric($iterations)) {
$error = "Not a valid number.";
global $error;
}
?>
我的錯誤可能來自一個無限循環,在這一行:
$result = $result+(((pow($x, $k))/(factorial($k)))*$operator);
但我不知道如何解決這個問題。 什麼我特林在這行做的是計算:
((pow($x, $k))/(factorial($k)) + (((pow($x, $k))/(factorial($k)) * ($operator)
迭代:
+ (((pow($x, $k))/(factorial($k)) * $operator)
次以 「$ I」 的和「$ k時的 「$迭代」 量「的價值觀相應改變。
我真的被困在這裏!需要一點幫助。先謝謝你 !
順便說一句:階乘函數不是我的。我在PhP.net的評論中發現它,顯然它是最優的因子函數。
您可能需要閱讀關於全球的[docs](http://php.net/global)。你不必宣佈每一個變量都是全局的,你做這件事的方式無論如何也無濟於事。 – 2012-04-15 03:43:23