How to implement (x pow y) in java,where x,y are double?
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;
精彩评论