Instance Property


The magnitude of this value.


var magnitude: Double { get }


For any value x, x.magnitude.sign is .plus. If x is not NaN, x.magnitude is the absolute value of x.

The global abs(_:) function provides more familiar syntax when you need to find an absolute value. In addition, because abs(_:) always returns a value of the same type, even in a generic context, using the function instead of the magnitude property is encouraged.

let targetDistance: Double = 5.25
let throwDistance: Double = 5.5

let margin = targetDistance - throwDistance
// margin == -0.25
// margin.magnitude == 0.25

// Use 'abs(_:)' instead of 'magnitude'
print("Missed the target by \(abs(margin)) meters.")
// Prints "Missed the target by 0.25 meters."


From Protocol

See Also

Finding the Sign and Magnitude

var sign: FloatingPointSign

The sign of the floating-point value.

typealias Double.Magnitude

A type that can represent the absolute value of any possible value of this type.