Swift Tutorial Swift References

Swift - nexttoward() Function



The Swift nexttoward() 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 nexttoward(_ __x: Double, _ __y: Float80) -> Double

Parameters

x Specify the base value.
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 nexttoward() function.

import Foundation

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

The output of the above code will be:

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

❮ Swift Math Functions