6

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));
}
Wilfred Hughes
  • 26,027
  • 13
  • 120
  • 177
Jerry Xiong
  • 69
  • 1
  • 2

3 Answers3

13

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.

inhan
  • 7,038
  • 1
  • 20
  • 35
Supericy
  • 5,735
  • 1
  • 18
  • 24
8

You need to use the pow($number, $exponent) function.

In PHP, the ^ 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));

Community
  • 1
  • 1
ixchi
  • 2,119
  • 2
  • 22
  • 23
4

^ is the XOR bitwise operator

You can use pow as mentioned by Supericy, but you can also just use the ** operator for shorthand.

Martin
  • 325
  • 3
  • 12