Instance Method インスタンスメソッド

negate()

Replaces this value with its additive inverse. この値をそれの加法の逆元で置き換えます。

Declaration 宣言

mutating func negate()

Discussion 解説

The following example uses the negate() method to negate the value of an integer x: 以下の例はnegate()メソッドを使って、整数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

Default Implementations 省略時実装

SignedNumeric Implementations