开发者

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

开发者 https://www.devze.com 2023-01-08 02:12 出处:网络
I want to calculate x power y and both x,y are double values. Why is ja开发者_StackOverflow社区va giving me a compilation error? What is the best way to do so?

I want to calculate x power y and both x,y are double values. Why is ja开发者_StackOverflow社区va giving me a compilation error? What is the best way to do so?

I am currently using the following method:

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

Thanks.


Math.pow(x, y);

Read the java.lang.Math docs.


The simplest way to implement it remains, as always:

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

To simply calculate it, Math.pow(x,y);, as has been pointed out.


Math.pow(x,y);

example:

Math.pow(2.23, 3.45);


        Math.pow(a, b);


See the Math class. It has a static function pow, which accepts double values as arguments.


    Double a = 3.0;
    Double b = 2.0;
    assert Math.pow(a, b) == 9.0;
0

精彩评论

暂无评论...
验证码 换一张
取 消