Swift - log() Function
The Swift log() function returns the natural logarithm (base-e) of a given number.
Syntax
In Foundation framework, it is defined as follows:
public func log(_ x: CGFloat) -> CGFloat public func log(_ x: Float) -> Float public func log(_ x: Float80) -> Float80 public func log(_ __x: Double) -> Double
Parameters
x |
Specify the number. |
Return Value
Returns the natural logarithm (base-e) of a given number.
If the x is negative, it returns -nan.
If the x is 0, it returns -inf.
Example:
In the example below, log() function is used to calculate the natural logarithm (base-e) of a given number.
import Foundation print("log(0.0) = \(log(0.0))") print("log(1.0) = \(log(1.0))") print("log(M_E) = \(log(M_E))") print("log(50.0) = \(log(50.0))") print("log(-1.0) = \(log(-1.0))")
The output of the above code will be:
log(0.0) = -inf log(1.0) = 0.0 log(M_E) = 1.0 log(50.0) = 3.912023005428146 log(-1.0) = -nan
❮ Swift Math Functions