java中的math pow_Java中的Math.pow()背后的算法是什么?

这是一个有趣的问题.如果您查看Java Math类的源代码,您会发现它调用StrictMath.pow(double1,double2),StrictMath的签名是public static native pow(double a,double b);

所以,最终,这是一个真正的本地电话,可能会因平台而有所不同.然而,在某个地方有一个实现,并不是很容易看.这里是函数的描述和函数本身的代码:

注意

看数学,试图理解它可能不可避免地会导致更多的问题.但是,通过在Java Math Function Source Code搜索这个Github并扫描数学概要,你可以更好地理解本机功能.快乐探索:)

方法说明

Method: Let x = 2 * (1+f)

1. Compute and return log2(x) in two pieces:

log2(x) = w1 + w2,

where w1 has 53-24 = 29 bit trailing zeros.

2. Perform y*log2(x) = n+y' by simulating muti-precision

arithmetic, where |y'|<=0.5.

3. Return x**y = 2**n*exp(y'*log2)

特殊情况

1. (anything) ** 0 is 1

2. (anything) ** 1 is itself

3. (anything) ** NAN is NAN

4. NAN ** (anything except 0) is NAN

5. +-(|x| > 1) ** +INF is +INF

6. +-(|x| > 1) ** -INF is +0

7. +-(|x| < 1) ** +INF is +0

8. +-(|x| < 1) ** -INF is +INF

9. +-1 ** +-INF is NAN

10. +0 ** (+anything except 0, NAN) is +0

11. -0 ** (+anything except 0, NAN, odd integer) is +0

12. +0 ** (-anything except 0, NAN) is +INF

13. -0 ** (-anything except 0, NAN, odd integer) is +INF

14. -0 ** (odd integer) = -( +0 ** (odd integer) )

15. +INF ** (+anything except 0,NAN) is +INF

16. +INF ** (-anything except 0,NAN) is +0

17. -INF ** (anything) = -0 ** (-anything)

18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)

19. (-anything except 0 and inf) ** (non-integer) is NAN

准确性

pow(x,y) returns x**y nearly rounded. In particular

pow(integer,integer)

always returns the correct integer provided it is

representable.

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值