Swift Tutorial Swift References

Swift - frexp() Function



The Swift frexp() function is used to break the floating point number x into its binary significand (a floating point with an absolute value in range [0.5, 1.0)) and an integral exponent for 2. Mathematically, it can be expressed as:

x = significand * 2exponent

The function returns (significand, exponent). If x is zero, both significand and exponent are zero.

Syntax

In Foundation framework, it is defined as follows:

public func frexp(_ x: CGFloat) -> (CGFloat, Int)

Parameters

x Specify the value to be decomposed.

Return Value

Returns binary significand and exponent of x.

Example:

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

import Foundation

print("frexp(8) = \(frexp(8))")
print("frexp(5) = \(frexp(5))")
print("frexp(25) = \(frexp(25))")
print("frexp(0) = \(frexp(0))")

The output of the above code will be:

frexp(8) = (0.5, 4)
frexp(5) = (0.625, 3)
frexp(25) = (0.78125, 5)
frexp(0) = (0.0, 0)

❮ Swift Math Functions