Swift Tutorial Swift References

Swift - logb() Function

The Swift logb() function returns the logarithm of |x| (mod of argument), using FLT_RADIX as base for the logarithm. On most platforms, FLT_RADIX is 2.


In Foundation framework, it is defined as follows:

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


x Specify the value to calculate logarithm.

Return Value

Returns the base-FLT_RADIX logarithm of |x|.


In the example below, logb() function is used to calculate the base-FLT_RADIX logarithm of absolute value of given numbers.

import Foundation

print("logb(0.0) = \(logb(0.0))")
print("logb(1.0) = \(logb(1.0))")
print("logb(10.0) = \(logb(10.0))")
print("logb(50.0) = \(logb(50.0))")
print("logb(-10.0) = \(logb(-10.0))")

The output of the above code will be:

logb(0.0) = -inf
logb(1.0) = 0.0
logb(10.0) = 3.0
logb(50.0) = 5.0
logb(-10.0) = 3.0

❮ Swift Math Functions