- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math sqrt(double x) Method
Description
The Java Math sqrt(double a) returns the correctly rounded positive square root of a double value. Special cases −
If the argument is NaN or less than zero, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is positive zero or negative zero, then the result is the same as the argument.
Otherwise, the result is the double value closest to the true mathematical square root of the argument value.
Declaration
Following is the declaration for java.lang.Math.sqrt() method
public static double sqrt(double a)
Parameters
a − a value.
Return Value
This method returns the positive square root of a. If the argument is NaN or less than zero, the result is NaN.
Exception
NA
Computing Square Root of a Positive Double Value Example
The following example shows the usage of Math sqrt() method to get a square root of a positive double value.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a double number double x = 1654.9874; // find the square root for this double number System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.sqrt(1654.9874)=40.68153635250272
Computing Square Root of a Negative Double Value Example
The following example shows the usage of Math sqrt() method to get a value for a negative double value.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a double number double x = -9765.134; // find the square root for this double number System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.sqrt(-9765.134)=NaN
Computing Square Root of a Zero Double Value Example
The following example shows the usage of Math sqrt() method to get a value for a zero double values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get double number double x = 0.0; // find the square root for this double number System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.sqrt(0.0)=0.0
To Continue Learning Please Login
Login with Google