Swift - scalbn() Function
The Swift scalbn() function returns the result of multiplying the significand (x) by FLT_RADIX raised to the power of the exponent (n). On most platforms, FLT_RADIX is 2. Mathematically, it can be expressed as:
scalbn(x,n) = x * FLT_RADIXn
Syntax
In Foundation framework, it is defined as follows:
public func scalbn(_ __x: Double, _ __n: Int32) -> Double
Parameters
x |
Specify the value representing the significand. |
n |
Specify the value of the exponent. |
Return Value
Returns x * FLT_RADIXn.
Example:
The example below shows the usage of scalbn() function.
import Foundation print("scalbn(0.8, 2) = \(scalbn(0.8, 2))") print("scalbn(5, 2) = \(scalbn(5, 2))") print("scalbn(2.0, 3) = \(scalbn(2.0, 3))") print("scalbn(3.0, 4) = \(scalbn(3.0, 4))")
The output of the above code will be:
scalbn(0.8, 2) = 3.2 scalbn(5, 2) = 20.0 scalbn(2.0, 3) = 16.0 scalbn(3.0, 4) = 48.0
❮ Swift Math Functions