开发者

PHP Math function returns wrong result

开发者 https://www.devze.com 2023-04-13 07:59 出处:网络
I have a variable containing: 40 * ($plvl^2)) 开发者_JAVA技巧+ (360 * $plvl); Where $plvl equals 2. According to wolfram and google the correct result is supposed to be 880 but the PHP function ret

I have a variable containing:

40 * ($plvl^2)) 开发者_JAVA技巧+ (360 * $plvl);

Where $plvl equals 2. According to wolfram and google the correct result is supposed to be 880 but the PHP function returns 720.

Does anyone know why they return different values and how do I correct it to result in 880?


$plvl^2

just inverts the second-lowest bit of plvl (^ is the bitwise XOR). You want pow:

40 * pow($plvl, 2) + 360 * $plvl;


The ^ operator is not exponentiation, it is the eXclusive OR (XOR) bitwise operator. Instead of $plvl^2, use pow($plvl, 2)


^ is XOR, not exponentiation. 2^2 is zero.


Just :

$plvl = 2;

echo 40*$plvl*$plvl+360*$plvl; //880

Not need the parentheses actually.


Use

40 * pow($plvl,2) + (360 * $plvl);

it works then. Maybe, the ^ operator is not a valid php operator.


In addition to the other answers: usually simple multiplication is way more efficient than using pow. So you should perhaps use $plvl * $plvl instead of pow($plvl,2).

0

精彩评论

暂无评论...
验证码 换一张
取 消