views:

83

answers:

1
+4  Q: 

PHP bizarre math

Hi,

I'm very new to PHP...and I'm getting unexpected values for variable calculations:

$var1 = $var2 * (((1 + $var3)^$var4)^$var5);

I've verified that $var2 is 3, $var3 is 0.1, $var4 is 1, $var5 is 1.1 so,

$var1 = 3*(((1+0.1)^1)^1.1) = 3.3316 but in PHP, $var1 = 3

if I change $var4 to 2,

$var1 = 3*(((1+0.1)^1)^1.1) = 3.6999 but in PHP, $var1 = 6

Why is this? Any ideas? I've tried explicitly declaring all variables as floats.

+11  A: 

Note that ^ is not "power of". You may want to have a look at the pow function.

(^ is actually "bitwise exclusive or".)

aioobe
`^` is the bitwise XOR operator: http://php.net/manual/en/language.operators.bitwise.php
deceze
Come on, enough with the up-voting. It's not rocket science. ;)
aioobe
Thank you!! (wow, you got 10 votes before I could accept the answer :)
Emile
this catches out a lot of newcomers to PHP (and even some more experienced folk from time to time).
Spudley