-2

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

Curious
  • 921
  • 1
  • 9
  • 25
  • Take a look at http://stackoverflow.com/questions/8071363/calculating-powers-in-java, it might answer both of your concerns. – Łukasz Nowak Jan 08 '17 at 06:52
  • 2
    The results of power computations are often irrational numbers that can only be approximated, and double provides the closest approximations with fast compact arithmetic. – Patricia Shanahan Jan 08 '17 at 07:59
  • Possible duplicate of [Why Does Math.pow(x,y) Count as a Double?](https://stackoverflow.com/questions/23326902/why-does-math-powx-y-count-as-a-double) – phuclv Oct 03 '17 at 12:58
  • Math.pow has also many corner cases one should be aware of: https://octoperf.com/blog/2018/03/16/java-math-pow/ – Jerome L Mar 18 '18 at 12:55

1 Answers1

2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    When is an integer to the power of another integer ever irrational? *Fractional* results (i.e., for negative exponents) *are* an issue, though. – dan04 Jan 09 '17 at 17:05