Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php, calculating exponent with caret (^) fails

Tags:

php

exponent

Wolfram Alpha says will give the correct result for the following formula:

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

But when I run it in PHP, it does not give the correct answer, why not?

$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));
}
like image 559
Jerry Xiong Avatar asked Jan 17 '13 02:01

Jerry Xiong


1 Answers

The ^ operator is a bitwise operator.

You need to use pow.

If you just want to square a value, then you can just multiple it by itself, $current * $current.

like image 154
Supericy Avatar answered Oct 22 '22 10:10

Supericy