Swift Tutorial Swift References

Swift - log10() Function



The Swift log10() function returns the base-10 logarithm of a given number.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify the number.

Return Value

Returns the base-10 logarithm 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, log10() function is used to calculate the base-10 logarithm of a given number.

import Foundation

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

The output of the above code will be:

log10(0.0) = -inf
log10(1.0) = 0.0
log10(10.0) = 1.0
log10(50.0) = 1.6989700043360187
log10(-1.0) = nan

❮ Swift Math Functions

5