Swift Tutorial Swift References

Swift - sqrt() Function

The Swift sqrt() function returns the square root of the given number.


In Foundation framework, it is defined as follows:

public func sqrt(_ x: CGFloat) -> CGFloat
public func sqrt(_ x: Float) -> Float
public func sqrt(_ x: Float80) -> Float80
public func sqrt(_ __x: Double) -> Double


x Specify a positive number.

Return Value

Returns the square root of the specified number.
If the x is negative, it returns -nan.


In the example below, sqrt() function is used to find out the square root of the given number.

import Foundation

print("sqrt(-1.0) = \(sqrt(-1.0))")
print("sqrt(25.0) = \(sqrt(25.0))")
print("sqrt(64.0) = \(sqrt(64.0))")
print("sqrt(100.0) = \(sqrt(100.0))")
print("sqrt(500.0) = \(sqrt(500.0))")

The output of the above code will be:

sqrt(-1.0) = -nan
sqrt(25.0) = 5.0
sqrt(64.0) = 8.0
sqrt(100.0) = 10.0
sqrt(500.0) = 22.360679774997898

❮ Swift Math Functions