Swift Tutorial Swift Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Swift - nextafter() Function



The Swift nextafter() function returns the next representable value after first argument in the direction of second argument.

Syntax

In Foundation framework, it is defined as follows:

public func nextafter(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat
public func nextafter(_ lhs: Float, _ rhs: Float) -> Float
public func nextafter(_ lhs: Float80, _ rhs: Float80) -> Float80
public func nextafter(_ __x: Double, _ __y: Double) -> Double

Parameters

lhs, x Specify the base value.
rhs, y Specify the value toward which the return value is approximated.

Return Value

Returns the next representable value after first argument in the direction of second argument.

Example:

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

import Foundation

print("nextafter(0.8, 1) = \(nextafter(0.8, 1))")
print("nextafter(0.8, 0) = \(nextafter(0.8, 0))")
print("nextafter(-0.8, -1) = \(nextafter(-0.8, -1))")
print("nextafter(-0.8, 0) = \(nextafter(-0.8, 0))")

The output of the above code will be:

nextafter(0.8, 1) = 0.8000000000000002
nextafter(0.8, 0) = 0.7999999999999999
nextafter(-0.8, -1) = -0.8000000000000002
nextafter(-0.8, 0) = -0.7999999999999999

❮ Swift Math Functions

5