You can find the square root with Math.sqrt()
. sqrt stands for Square root, which means square root.
Math.sqrt()
has the form:
public static double sqrt(double a)
Features include:
- Passing a as an argument returns the square root of a
- If 0 is passed as an argument, 0 is returned.
- If you pass a negative number or NaN (Not a Number) as an argument, NaN is returned.
Example
You can calculate the square root as
double x = 25;
double y = 225;
System.out.println("Math.sqrt(x)=" + Math.sqrt(x));
System.out.println("Math.sqrt(y)=" + Math.sqrt(y));
Output:
Math.sqrt(x)=5.0
Math.sqrt(y)=15.0
If you pass a negative number as an argument, NaN is returned, like this:
double negative = -5;
result = Math.sqrt(negative);
System.out.println(result);
Output:
NaN
Infinity is returned when an infinite value is passed as an argument as follows.
double infinity = Double.POSITIVE_INFINITY;
result = Math.sqrt(infinity);
System.out.println(result);
Output:
Infinity
If you pass NaN as an argument as follows, NaN is returned.
double nan = Double.NaN;
result = Math.sqrt(nan);
System.out.println(result);
Output:
NaN
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)