Power takeover in PHP

Well, I need to do some calculations in a PHP script. And I have one expression that is misbehaving.

echo 10^(-.01);

Outputs 10

echo 1 / (10^(.01));

Outputs 0

echo bcpow('10', '-0.01') . '<br/>';

Outputs 1

echo bcdiv('1', bcpow('10', '0.01'));

Outputs 1.000 ....

I use bcscale(100)BCMath for calculations.

Excel and Wolfram Mathematica give an answer of ~ 0.977237.

Any suggestions?

+3
source share
5 answers

A carriage is a bit-wise XOR operator in PHP. You must use pow()for integers.

+11
source

PHP 5.6, , , (**) - ^, XOR.

5.6:

$power = pow(2, 3);  // 8

5.6 :

$power = 2 ** 3;

:

$power   = 2 ** 2;
$power **=      2;  // 8

, - ( ), (~).

$a = 2 **  3 ** 2;  // 512, not 64 because of right-associativity
$a = 2 ** (3 ** 2); // 512

$b = 5 - 3 ** 3;    // -22 (power calculated before subtraction)

, - , (-), :

$b = -2 ** 2;        // -4, same as writing -(2 ** 2) and not 4
+6

^ XOR. pow, bcpow gmp_pow:

var_dump(pow(10, -0.01));  // float(0.977237220956)
+4
0

Since 2014 and the PHP 5.6 alpha update, there are many features included that I hope reach the final version of PHP. This is the operator **.

So you can do 2 ** 8to receive 256. PHP Docs say: "The correct associative operator has been added to support exponentiality **."

0
source

Source: https://habr.com/ru/post/1714041/


All Articles