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怎么做幂运算