Swift Tutorial Swift References

Swift - log1p() Function

The Swift log1p() function returns the natural logarithm of (1 + number), i.e., log(1+number).


In Foundation framework, it is defined as follows:

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


x Specify the number.

Return Value

Returns the natural logarithm of (1 + x), i.e., log(1+x).
If the x is less than -1, it returns -nan.
If the x is -1, it returns -inf.


In the example below, log1p() function is used to calculate the log(1+number).

import Foundation

print("log1p(-1.0) = \(log1p(-1.0))")
print("log1p(0.0) = \(log1p(0.0))")
print("log1p(10.0) = \(log1p(10.0))")
print("log1p(50.0) = \(log1p(50.0))")
print("log1p(-2.0) = \(log1p(-2.0))")

The output of the above code will be:

log1p(-1.0) = -inf
log1p(0.0) = 0.0
log1p(10.0) = 2.3978952727983707
log1p(50.0) = 3.9318256327243257
log1p(-2.0) = -nan

❮ Swift Math Functions