Instance Method (Default implementation)swift 6.0.1Swift
negate
Replaces this value with its additive inverse.
mutating func negate()
The following example uses the negate()
method to negate the value of an integer x
:
var x = 21
x.negate()
// x == -21
The resulting value must be representable within the value’s type. In particular, negating a signed, fixed-width integer type’s minimum results in a value that cannot be represented.
var y = Int8.min
y.negate()
// Overflow error
Implements
func negate(
) Replaces this value with its additive inverse.
Other members in extension
Type members
static func - (operand: Self
) -> Self Returns the additive inverse of the specified value.