Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Math sqrt: Calculating Square Roots in Java

A practical guide to Java's Math.sqrt method: syntax, return behavior for negative and NaN inputs, precision concerns, and alternatives such as BigDecimal.sqrt.

~4 min readJava
#Math.sqrt#Java Math#Square Root#Double Precision
A Java square root calculation showing Math.sqrt returning a double value from a numeric input.

The square root operation in Java is implemented by the static method Math.sqrt(double). It returns the positive square root of a double value, with specific handling for edge cases such as negative numbers, NaN, infinity, and zero. The method is part of java.lang.Math, so no import is required.

The Math.sqrt Method Signature and Behavior

Math.sqrt has a single overload:

java
public static double sqrt(double a)

It accepts any double and returns a double. The result is the positive square root of the argument, rounded to the nearest representable double value. The method follows IEEE 754 semantics for special inputs:

ArgumentReturn value
Positive finitePositive square root
ZeroZero with the same sign as the argument
Negative finiteNaN
Negative infinityNaN
NaNNaN
Positive infinityPositive infinity

The zero case is worth noting: Math.sqrt(-0.0) returns -0.0, while Math.sqrt(0.0) returns 0.0. This distinction rarely matters in practice, but it is part of the IEEE 754 specification and can affect calculations that treat signed zero differently, such as a reciprocal.

Basic Usage with int and double

Passing a double literal or variable is the most direct use:

java
double root = Math.sqrt(16.0); System.out.println(root); // 4.0

An int argument is automatically widened to double before the method is called, so Math.sqrt(25) works and returns 5.0. If you need an integer result, you must cast explicitly:

java
int side = (int) Math.sqrt(49);

The cast truncates any fractional part. For perfect squares this is safe, but for non-perfect squares you will lose the decimal portion. Use Math.round if rounding is required.

Handling Negative Inputs and NaN

A negative argument does not throw an exception; it returns NaN. This is a common source of bugs because NaN is not equal to itself, and it can propagate silently through calculations. Always check the result with Double.isNaN when the input might be negative:

java
double value = -9.0; double root = Math.sqrt(value); if (Double.isNaN(root)) { // handle invalid input }

The same check works when the argument itself is NaN. Because NaN can arise from earlier operations, validating the result of Math.sqrt is more robust than checking the input alone.

Precision and Rounding Considerations

Math.sqrt returns a double, which has 53 bits of significand precision. For perfect squares that fit exactly in the double range, the result is exact. For other values, the result is the closest representable double, within 1 ulp of the true square root.

If you need more precision, such as for financial or scientific calculations, double may not be sufficient. In that case, use BigDecimal.sqrt, available since Java 9:

java
import java.math.BigDecimal; import java.math.MathContext; BigDecimal value = new BigDecimal("2"); BigDecimal root = value.sqrt(new MathContext(50));

This gives a square root with a controlled number of significant digits, but it is significantly slower than Math.sqrt because it uses arbitrary-precision arithmetic. Use it only when the extra precision is necessary.

Alternatives: Math.pow and BigDecimal.sqrt

Math.pow(x, 0.5) also computes a square root, but it is a general exponentiation method and is not optimized for the specific case of a square root. Math.sqrt is clearer and avoids the overhead of the general path, though the actual difference depends on the JVM and hardware.

BigDecimal.sqrt is the right choice when you need arbitrary precision or when you are already working with BigDecimal values. It requires a MathContext to specify the precision. The result is rounded according to the rounding mode in the MathContext, which defaults to HALF_UP for the MathContext constructor used above.

Performance and Runtime Behavior

On modern JVMs, Math.sqrt often maps to a hardware square root instruction, making it very fast. However, unlike StrictMath.sqrt, Math.sqrt is not specified to return the same bit-for-bit result across all JVM implementations. This means two JVMs on different hardware could produce results that differ in the last bit. For most applications this is irrelevant, but if you need reproducible results across platforms, use StrictMath.sqrt instead.

Math.sqrt is also thread-safe because it is a stateless static method; it does not maintain any internal state. There is no need to synchronize calls to it.

Common Mistakes and Edge Cases

One frequent mistake is comparing the result of Math.sqrt to NaN with ==:

java
if (Math.sqrt(-1) == Double.NaN) { // always false }

NaN is not equal to itself, so this condition never fires. Use Double.isNaN.

Another issue is casting the result to int without considering rounding. For example, Math.sqrt(15) returns approximately 3.873, and (int) 3.873 becomes 3. If conventional rounding to the nearest integer is what you need, use Math.round, which gives 4.

Finally, remember that Math.sqrt accepts only double. For float inputs, the value is widened to double, and the result is double. If you need a float, cast the result, but be aware that you lose precision.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.