scala怎么做幂运算_Scala幂(幂)函数示例
scala怎么做冪運(yùn)算
Scala programming language has a huge set of libraries to support different functionalities.
Scala編程語(yǔ)言具有大量的庫(kù)來(lái)支持不同的功能。
scala.math.pow() (scala.math.pow())
The pow() function is used for the exponential mathematical operation,
pow()函數(shù)用于指數(shù)數(shù)學(xué)運(yùn)算,
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庫(kù)訪問(wèn)此方法。 該函數(shù)接受兩個(gè)變量,第一個(gè)是數(shù)字,第二個(gè)是要找到其日期指數(shù)的數(shù)字的冪。 并且,它返回帶指數(shù)函數(shù)結(jié)果的雙精度整數(shù)。
Let us see,?the usage of pow() function and how to implement this into a Scala program?
讓我們看看pow()函數(shù)的用法以及如何將其實(shí)現(xiàn)到Scala程序中?
Example 1: Program to find square of a number in Scala
示例1:在Scala中查找數(shù)字平方的程序
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.0Code explanation:
代碼說(shuō)明:
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.
上面的代碼是查找給定數(shù)字的平方。 為了找到給定數(shù)字的平方,我們將2傳遞給pow()函數(shù)的第二個(gè)值。 返回?cái)?shù)字冪2的平方。
Example 2: Program to find square root of a number in Scala
示例2:在Scala中查找數(shù)字平方根的程序
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.0Code explanation:
代碼說(shuō)明:
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.
上面的代碼用于查找給定數(shù)字的平方根。 在此程序中,我們使用了Scala庫(kù)中的pow()函數(shù) 。 該函數(shù)接受兩個(gè)double值,并將double值返回為pow()函數(shù)的輸出。 為了找到平方根,我們將第二個(gè)值設(shè)置為0.5 ,該值給出了數(shù)字的平方根。 使用println語(yǔ)句在下一行中打印平方根。
翻譯自: https://www.includehelp.com/scala/power-exponentiation-function-with-example-in-scala.aspx.aspx
scala怎么做冪運(yùn)算
總結(jié)
以上是生活随笔為你收集整理的scala怎么做幂运算_Scala幂(幂)函数示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 加盟中通快递多少钱啊?
- 下一篇: 宽带要多少钱啊?