In Swift 3, the FloatingPoint protocol has a squareRoot() method. Both Float and Double compliant with the FloatingPoint protocol. So:
let x = 4.0 let y = x.squareRoot()
about as simple as him.
The generated code should be based on one x86 machine instruction, without going to the function address, and then return, because this translates to LLVM, which is built into the intermediate code. Thus, it should be faster than calling the C sqrt library function, which is really a function, not just a macro for assembly code.
In Swift 3, you do not need to import anything to make this work.
Paul buis
source share