2013-01-17 62 views
5

Wolfram Alpha的說將給予以下公式正確的結果:PHP,計算指數胡蘿蔔(^)失敗

((0.0004954*($current^2))-((0.935*$current)+378.486))- 
((0.0004954*($desired^2))-((0.935*$desired)+378.486)); 

但是當我在PHP中運行它,它並沒有給出正確的答案,爲什麼不?

$current = mysql_escape_string($_POST['current']); 
$desired = mysql_escape_string($_POST['desired']); 
$username = mysql_escape_string($_POST['username']); 
$password = mysql_escape_string($_POST['password']); 
$email = mysql_escape_string($_POST['email']); 
$ip = $_SERVER["REMOTE_ADDR"]; 
$time = time(); 
$elo = $desired - $current; 
if($current < 1200) { 
    $price = ($elo/100)*30; 
} elseif($current < 1400) { 
    $price = ($elo/100)*35; 
} elseif($current < 1901) { 
    $price = ((0.0004954*($current^2))-((0.935*$current)+378.486))-((0.0004954*($desired^2))-((0.935*$desired)+378.486)); 
} 

回答

11

^運算符是一個bitwise運營商。你需要使用pow

如果您只想對一個值進行平方,那麼您可以單獨將其倍數,$current * $current

6

您需要使用pow($number, $exponent)函數。

在PHP中,^doesn't mean an exponent

$price = ((0.0004954*(pow($current, 2)))-((0.935*$current)+378.486))-((0.0004954* (pow($desired, 2)))-((0.935*$desired)+378.486));

0

^是按位異或操作

由Supericy提到可以使用戰俘,但你也可以只使用**運算符的簡寫。