Swift Tutorial Swift References

Swift - ilogb() Function



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

Syntax

In Foundation framework, it is defined as follows:

public func ilogb(_ x: CGFloat) -> Int
public func ilogb<T>(_ x: T) -> Int where T : BinaryFloatingPoint
public func ilogb(_ __x: Double) -> Int32

Parameters

x Specify the value to calculate logarithm.

Return Value

Returns the integer part of the logarithm of |x|, using FLT_RADIX as base for the logarithm.

Example:

The example below shows the usage of ilogb() function.

import Foundation

print("ilogb(1.0) = \(ilogb(1.0))")
print("ilogb(10.0) = \(ilogb(10.0))")
print("ilogb(50.0) = \(ilogb(50.0))")
print("ilogb(-10.0) = \(ilogb(-10.0))")
print("ilogb(-50.0) = \(ilogb(-50.0))")

The output of the above code will be:

ilogb(1.0) = 0
ilogb(10.0) = 3
ilogb(50.0) = 5
ilogb(-10.0) = 3
ilogb(-50.0) = 5

❮ Swift Math Functions

5