【问题标题】:Calculating roots with bc_math or GMP使用 bc_math 或 GMP 计算根
【发布时间】:2010-12-04 01:54:24
【问题描述】:

我在使用 bc_math 计算相当大数的根时遇到问题,例如:

 - pow(2, 2)        // 4, power correct
 - pow(4, 0.5)      // 2, square root correct
 - bcpow(2, 2)      // 4, power correct
 - bcpow(4, 0.5)        // 1, square root INCORRECT

有谁知道我可以如何规避这个问题? gmp_pow() 也不起作用。

【问题讨论】:

标签: php math gmp bcmath


【解决方案1】:

我不是 PHP 程序员,但看着 the manual 它说你必须将它们作为字符串传递,即

bcpow( '4', '0.5' )

这有帮助吗?

编辑:user contributed notes in the manual page 确认它不支持非整数指数。

我在快速搜索后确实遇到了这个discussion of a PHP N-th root algorithm,所以也许这就是您需要的。

【讨论】:

  • 不,它仍然输出 1 而不是 2。
  • 是的,bcsqrt 有效,但我还需要使用其他索引(例如 3、5 等)查找根。
  • @eyze:是的,我知道。我只是想检查它是否正常工作。
  • 您提供的最后一个链接很有用,但是它错误地返回 1.5 作为 2 的平方根,还有更精确的解决方法吗?
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
相关资源
最近更新 更多