solve square root in swift

To solve square root in Swift, you can use the sqrt function provided by the Swift's standard library.

Here's an example code snippet to find the square root of a number:

main.swift
let number = 16.0
let squareRoot = sqrt(number)
print(squareRoot) // output: 4.0
81 chars
4 lines

The sqrt function takes a single argument of type Double and returns its square root as a Double value.

If you want to calculate square root of a negative number, it'll return NaN (not a number).

main.swift
let negativeNumber = -16.0
let squareRootOfNegative = sqrt(negativeNumber)
print(squareRootOfNegative) // output: NaN
118 chars
4 lines

related categories

gistlibby LogSnag