Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP bizarre math

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.

like image 676
Kyle Cureau Avatar asked Dec 13 '22 19:12

Kyle Cureau


1 Answers

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

(^ is actually "bitwise exclusive or".)

like image 54
aioobe Avatar answered Dec 27 '22 22:12

aioobe