Generic Initializer

init(clamping:)

Creates a new instance with the representable value that’s closest to the given integer. 与えられた整数に最も近い表現可能な値を使って新しいインスタンスを作成します。

Declaration 宣言

init<Other>(clamping source: Other) where Other : BinaryInteger

Parameters パラメータ

source

An integer to convert to this type. この型へと変換する整数。

Discussion 解説

If the value passed as source is greater than the maximum representable value in this type, the result is the type’s max value. If source is less than the smallest representable value in this type, the result is the type’s min value. sourceとして渡された値がこの型において最大限の表現可能な値より大きいならば、結果はこの型のもつmax値です。sourceがこの型の最も小さい表現可能な値より小さいならば、結果はこの型のもつmin値です。

In this example, x is initialized as an Int8 instance by clamping 500 to the range -128...127, and y is initialized as a UInt instance by clamping -500 to the range 0...UInt.max. この例において、xInt8インスタンスとして、500を範囲-128...127に制限すること(クランピング)によって初期化されます、そしてyUIntインスタンスとして、-500を範囲0...UInt.maxに制限することによって初期化されます。


let x = Int8(clamping: 500)
// x == 127
// x == Int8.max


let y = UInt(clamping: -500)
// y == 0