Julia Square Root Using sqrt()
Use Julia’s sqrt() function to calculate the principal square root of a number. It works with nonnegative integers, floating-point values, and complex numbers.
The basic syntax is:
sqrt(x)
Here, x is the value whose square root must be calculated. For integer and floating-point input, sqrt() normally returns a floating-point result.
Calculate the Square Root of an Integer in Julia
The following Julia REPL session calculates square roots of integer values. A perfect square such as 25 produces 5.0, while a non-perfect square such as 23 produces an approximate floating-point result.
julia> x = 25
25
julia> sqrt(x)
5.0
julia> x = 23
23
julia> sqrt(x)
4.795831523312719
Even when the input is an integer, sqrt() returns a floating-point value because many integers do not have an exact integer square root.
Calculate the Square Root of a Floating-Point Number
sqrt() can be called directly with a floating-point value.
julia> x = 2.2
2.2
julia> sqrt(x)
1.4832396974191326
The result is an approximation represented using Julia’s floating-point number type.
Calculate the Square Root of a Complex Number
Julia also supports square roots of complex numbers. The imaginary unit is written as im.
julia> x = 2 + 3im
2 + 3im
julia> sqrt(x)
1.6741492280355401 + 0.8959774761298381im
For a complex input, sqrt() returns the principal complex square root.
Square Root of a Negative Number in Julia
A negative real number does not have a real square root. Calling sqrt() with a negative real value raises a domain error.
x = -9
sqrt(x)
Convert the value to a complex number when a complex result is required:
x = -9 + 0im
result = sqrt(x)
println(result)
0.0 + 3.0im
Apply sqrt() to Every Element of a Julia Array
sqrt() accepts one number at a time. To calculate the square root of every element in an array, use Julia’s dot-broadcasting syntax sqrt.(values).
values = [1, 4, 9, 16, 25]
roots = sqrt.(values)
println(roots)
[1.0, 2.0, 3.0, 4.0, 5.0]
The dot before the parentheses tells Julia to apply sqrt() element by element.
Use isqrt() for an Integer Square Root
Use isqrt() when an integer result is required. It returns the greatest integer whose square is less than or equal to the input value.
println(isqrt(25))
println(isqrt(23))
5
4
For 23, the integer square root is 4 because 4^2 = 16 and 5^2 = 25, which is greater than 23.
Check Whether a Julia Integer Is a Perfect Square
An integer is a perfect square when squaring its integer square root reproduces the original value.
function is_perfect_square(n::Integer)
n < 0 && return false
root = isqrt(n)
return root * root == n
end
println(is_perfect_square(49))
println(is_perfect_square(50))
true
false
This approach avoids comparing approximate floating-point results.
sqrt(), isqrt(), and the Exponent Operator
| Julia expression | Purpose | Typical result |
|---|---|---|
sqrt(x) | Calculate the principal square root | Floating-point or complex value |
isqrt(n) | Calculate the integer square root of a nonnegative integer | Integer |
x^(1/2) | Raise a value to the power one-half | Usually floating-point |
For ordinary square-root calculations, sqrt(x) is clearer than writing x^(1/2). Use isqrt() when integer arithmetic is specifically needed.
Common Julia Square Root Errors
- Calling
sqrt()on a negative real number without converting it to a complex number. - Using
sqrt(array)when element-wise broadcasting withsqrt.(array)is intended. - Expecting
sqrt(25)to return the integer5instead of the floating-point value5.0. - Using floating-point equality to test whether a large integer is a perfect square instead of using
isqrt().
Julia Square Root FAQs
How do you calculate a square root in Julia?
Pass the number to sqrt(). For example, sqrt(36) returns 6.0.
Why does sqrt(25) return 5.0 instead of 5?
sqrt() uses a floating-point result for ordinary integer inputs because many integers have non-integer square roots. Use isqrt(25) when an integer result is required.
How do you find the square root of a negative number in Julia?
Represent the value as a complex number, such as sqrt(-9 + 0im). The result is 0.0 + 3.0im.
How do you calculate square roots of all values in a Julia vector?
Use broadcasting with a dot: sqrt.([1, 4, 9]). This returns [1.0, 2.0, 3.0].
What is the difference between sqrt() and isqrt() in Julia?
sqrt() calculates a numerical square root and can return a floating-point or complex value. isqrt() returns the floor of the exact square root as an integer for a nonnegative integer input.
Julia Square Root Tutorial Summary
Use sqrt() for standard real or complex square-root calculations, sqrt.() broadcasting for arrays, and isqrt() for integer square roots. In this Julia Tutorial, we used each form with examples and covered negative values, vectors, and perfect-square checks.
TutorialKart.com