java.lang.StrictMath.sqrt()方法實例
java.lang.StrictMath.sqrt() 方法返回double值的正平方根。它包括以下情況:
- 如果參數爲NaN或小於零,那麼結果爲NaN。
- 如果參數爲正無窮大,那麼結果爲正無窮大。
- 如果參數爲正零或負零,那麼結果與參數一樣。
否則,其結果是最接近參數值的真實數學平方根的double值。
聲明
以下是**java.lang.StrictMath.sqrt()**方法的聲明
public static double sqrt(double a)
參數
- a -- 這是所使用的值。
返回值
此方法返回正平方的根。
異常
- NA
例子
下面的例子顯示java.lang.StrictMath.sqrt()方法的使用。
package com.yiibai; import java.lang.*; public class StrictMathDemo { public static void main(String[] args) { double d1 = 121 , d2 = -729, d3 = 0.0; // returns the positive square root of a double value. double sqrtValue = StrictMath.sqrt(d1); System.out.println("square root of " + d1 + " = " + sqrtValue); sqrtValue = StrictMath.sqrt(d2); System.out.println("square root of " + d2 + " = " + sqrtValue); sqrtValue = StrictMath.sqrt(d3); System.out.println("square root of " + d3 + " = " + sqrtValue); } }
讓我們來編譯和運行上面的程序,這將產生以下結果:
square root of 121.0 = 11.0 square root of -729.0 = NaN square root of 0.0 = 0.0