Instance Method

distance(to:)

Returns the distance from this value to the specified value.

Declaration

func distance(to other: Float) -> Float

Parameters

other

A value to calculate the distance to.

Return Value

The distance between this value and other.

Discussion

For two values x and y, the result of x.distance(to: y) is equal to y - x—a distance d such that x.advanced(by: d) approximates y. For example:

let x = 21.5
let d = x.distance(to: 15.0)
// d == -6.5

print(x.advanced(by: d))
// Prints "15.0"

Relationships

From Protocol

See Also

Infrequently Used Functionality

init()

Creates a value initialized to zero.

init(integerLiteral: Int64)

Creates a new value from the given integer literal.

init(floatLiteral: Float)

Creates a new value from the given floating-point literal.

init(integerLiteral: Float)

Creates an instance initialized to the specified integer value.

func advanced(by: Float) -> Float

Returns a new value advanced by the given distance.

func write<Target>(to: inout Target)

Writes a textual representation of this instance into the given output stream.