How to implement (x pow y) in java, where x, y are double?

I want to calculate x power y , and x, y - double . Why does java give me a compilation error? What is the best way to do this?

I am currently using the following method:

x^y // attempt to calculate (x pow y)

Thank.

+3
source share
6 answers

The easiest way to implement it remains, as always:

Take the logarithm (base 10) x; multiply it by y and take the inverse logarithm (base of 10) of the result to get x pow y.

To just calculate it Math.pow(x,y);, as indicated.

+1
source
Math.pow(x, y);

Read the docsjava.lang.Math .

+8
source
Math.pow(x,y);

:

Math.pow(2.23, 3.45);
+1
        Math.pow(a, b);
+1

See the Math class . It has a static pow function that takes double values ​​as arguments.

+1
source
    Double a = 3.0;
    Double b = 2.0;
    assert Math.pow(a, b) == 9.0;
+1
source

Source: https://habr.com/ru/post/1754952/


All Articles