What's the difference between:
Math.pow ( x,y ); // x^y
To:
x^y; // x^y
?
Will I prefer to use x^y
with double
type numbers? Or shell I have to use always with Math.pow()
method?
Java Program to Calculate Power of X to Y The function returns the power of x^y. Even though you can write this kind of function quickly, it's far from ideal, and if you really need a power function for your production code, I suggest you use the Math. pow() function because it's thoroughly tested.
pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter. There are some special cases as listed below: If the second parameter is positive or negative zero then the result will be 1.0.
Write a function pow(x,n) that returns x in power n . Or, in other words, multiplies x by itself n times and returns the result. pow(3, 2) = 3 * 3 = 9 pow(3, 3) = 3 * 3 * 3 = 27 pow(1, 100) = 1 * 1 * ...
^
is the bitwise exclusive OR (XOR) operator in Java (and many other languages). It is not used for exponentiation. For that, you must use Math.pow
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With