PHP 的崛起

发布于 2024-07-29 16:26:32 字数 441 浏览 7 评论 0原文

好吧,我需要在 PHP 脚本中做一些计算。 我有一种表达方式是错误的。

echo 10^(-.01);

输出 10

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

输出 0

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

输出 1

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

输出 1.000...

我正在使用 bcscale(100) 进行 BCMath 计算。

Excel 和 Wolfram Mathematica 给出答案 ~0,977237。

有什么建议么?

Well, i need to do some calculations in PHP script. And i have one expression that behaves wrong.

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'm using bcscale(100) for BCMath calculations.

Excel and Wolfram Mathematica give answer ~0,977237.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执着的年纪 2024-08-05 16:26:32

插入符号是 PHP 中的按位 XOR 运算符。 您需要使用 pow() 来表示整数。

The caret is the bit-wise XOR operator in PHP. You need to use pow() for integers.

默嘫て 2024-08-05 16:26:32

PHP 5.6 最终引入了一个固有的幂运算符,用双星号 (**) 表示 - 不要与按位异或运算符 ^ 混淆。

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

PHP 5.6 finally introduced an innate power operator, notated by a double asterisk (**) - not to be confused with ^, the bitwise XOR operator.

Before 5.6:

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

5.6 and above:

$power = 2 ** 3;

An assignment operator is also available:

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

Through many discussions and voting, it was decided that the operator would be right-associative (not left) and its operator precedence is above the bitwise not operator (~).

$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)

Also, for some reason that does not make much sense to me, the power is calculated before the negating unary operator (-), thus:

$b = -2 ** 2;        // -4, same as writing -(2 ** 2) and not 4
树深时见影 2024-08-05 16:26:32

^ 运算符是按位异或运算符< /a>. 您必须使用 powbcpowgmp_pow

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

The ^ operator is the bitwise XOR operator. You have to use either pow, bcpow or gmp_pow:

var_dump(pow(10, -0.01));  // float(0.977237220956)
ら栖息 2024-08-05 16:26:32

bcpow 函数 仅支持整数指数。 尝试使用 pow 代替。

The bcpow function only supports integer exponents. Try using pow instead.

自我难过 2024-08-05 16:26:32

截至 2014 年,PHP 5.6 alpha 更新中包含了很多功能,我希望这些功能能够出现在 PHP 的最终版本中。 它是 ** 运算符。

因此,您可以执行 2 ** 8 得到 256。 PHP 文档说:“添加了右关联 ** 运算符以支持求幂”。

As of 2014, and the PHP 5.6 alpha update, there's a much included feature that I hope makes it to the final release of PHP. It's the ** operator.

So you can do 2 ** 8 will get you 256. PHP Docs say: "A right associative ** operator has been added to support exponentiation".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文