scala怎么做冪運算
Scala programming language has a huge set of libraries to support different functionalities.
Scala編程語言具有大量的庫來支持不同的功能。
scala.math.pow() (scala.math.pow())
The pow() function is used for the exponential mathematical operation,
pow()函數用于指數數學運算,
This method can be accessed from scala.math library directly.?The function accepts two variables First number and second the power of the number up to which date exponent is to be found.?And, it returns a double integer with the result of the exponential function.
可以直接從scala.math庫訪問此方法。 該函數接受兩個變量,第一個是數字,第二個是要找到其日期指數的數字的冪。 并且,它返回帶指數函數結果的雙精度整數。
Let us see,?the usage of pow() function and how to implement this into a Scala program?
讓我們看看pow()函數的用法以及如何將其實現到Scala程序中?
Example 1: Program to find square of a number in Scala
示例1:在Scala中查找數字平方的程序
object myClass{
def main(args: Array[String]) {
var i = 5;
var p = 2;
var ans = scala.math.pow(i,p)
println("The value of "+i+" to the power of "+p+" is "+ ans)
}
}
Output
輸出量
The value of 5 to the power of 2 is 25.0
Code explanation:
代碼說明:
The above code is to find the square of the given number.?To the find square of a given number, we will pass 2 to the second value of the pow() function.?which returns the numbers power 2 that is its square.
上面的代碼是查找給定數字的平方。 為了找到給定數字的平方,我們將2傳遞給pow()函數的第二個值。 返回數字冪2的平方。
Example 2: Program to find square root of a number in Scala
示例2:在Scala中查找數字平方根的程序
object myClass{
def main(args: Array[String]) {
var i = 25;
var p = 0.5;
var ans = scala.math.pow(i,p)
println("The value of "+i+" to the power of "+p+" is "+ ans)
}
}
Output
輸出量
The value of 25 to the power of 0.5 is 5.0
Code explanation:
代碼說明:
The above code is used to find the square root of the given number.?In this program, we have used the pow() function from the Scala library. the function takes two double values and return the double value as the output of the pow() function.?To find the square root we have set the second value to 0.5, which gives the square root of the number. The square root is printed in the next line using the println statement.
上面的代碼用于查找給定數字的平方根。 在此程序中,我們使用了Scala庫中的pow()函數 。 該函數接受兩個double值,并將double值返回為pow()函數的輸出。 為了找到平方根,我們將第二個值設置為0.5 ,該值給出了數字的平方根。 使用println語句在下一行中打印平方根。
翻譯自: https://www.includehelp.com/scala/power-exponentiation-function-with-example-in-scala.aspx.aspx
scala怎么做冪運算