Math.sqrt - Square Root in Java

Math.sqrt - Square Root in Java

Introduction

Calculating the square root of a number is one of the common operations we do in computer science. This simple mathematical function finds its use in all areas of programming - be it in algorithms or any other mathematical model we wish to represent, we'll most likely use square roots in some way.

There are many ways in which we can calculate the square root of a number in Java, but the main topic of this article is the sqrt() method from the Math class.

The Math class in Java 8

The Math class contains methods for performing a variety of basic numeric operations such as logarithmic, square root, min and max, trigonometric functions etc. However, as stated earlier, the topic of this article is the sqrt() method implemented within this class. The sqrt() method has the following syntax:

public static double sqrt(double a)

It's a pretty straightforward method, taking in a single double and returning a double as well. The method itself returns the correctly rounded positive square root of a double value. There are, however, a few special cases that require our attention while using this method:

  • If the argument is NaN or less than 0, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is negative infinity, then the result is NaN.
  • If the argument is positive 0 or negative 0, then the result is the same as the argument given.

Otherwise, the result is the double value closest to the true mathematical square root of the argument value. There really isn't much more to discuss with this method, so we can jump straight into an example:

double positiveNumberSqrt = Math.sqrt(137.4); //11.721774609674084
double negativeNumberSqrt = Math.sqrt(-137.4); // NaN
double invalidValueSqrt = Math.sqrt(0.0/0.0); // NaN
double positiveInfSqrt = Math.sqrt(Double.POSITIVE_INFINITY); // Infinity
double negativeInfSqrt = Math.sqrt(Double.NEGATIVE_INFINITY); // NaN
double positiveZeroSqrt = Math.sqrt(0.0); // 0.0
double negativeZeroSqrt = Math.sqrt(-0.0); // -0.0

Conclusion

As you've seen in this short article, we used the Math.sqrt() method to showcase just how simple it is to find the square root of a number in Java. Of course, this isn't the only way to find the square root of a number in Java, but it's certainly the most elegant and the easiest one. We've also discussed the Math class a bit, which is also filled with a variety of mathematical functions that can fulfill the needs of most of our needs when it comes to math-related concepts.

Last Updated: June 9th, 2022
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

Want a remote job?

    © 2013-2022 Stack Abuse. All rights reserved.

    DisclosurePrivacyTerms