为什么java Math.pow参数加倍? [英] why java Math.pow arguments double?

查看:338
本文介绍了为什么java Math.pow参数加倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java中的Math.pow API有双重参数,那么使用double的写法是什么原理呢?

为了计算浮点数,四舍五入错误等等。都涉及到为什么没有单独的API来计算整数?

为什么没有单独的方法
<解决方案

结果pow往往是非理性的,分数或太大,作为 long 存储,如果你想使用您可以使用的整数的权力

  BigInteger bi = BigInteger.valueOf(100); 
BigInteger bi2 = bi.pow(20); // 10 ^ 40

另一个原因可能是Math有许多从C中取的函数,来自常见的CPU指令集。 C只有一个浮点型方法,而x64只有浮点型的pow。


why Math.pow API in java is with double arguments, what is the rationale or use of writing with double ?

As caluclating the floats and double involve precision, round off errors etc.. are involved why not there is no separate API to caluclate with integers ?

Why there is no separate method

解决方案

The results of pow are often irrational, fractional or too large to store as a long, If you want to use powers of integers you can use

 BigInteger bi = BigInteger.valueOf(100);
 BigInteger bi2 = bi.pow(20); // 10^40

Another reason maybe that Math has many function which are taken from C, and also from common CPU instructions sets. C only has a floating point method and x64 only has the floating point version of pow.

这篇关于为什么java Math.pow参数加倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆