int16
func int16[T: Number](x: T): int16int16 converts x to int16 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; int16(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
int16holds the whole numbers from-32768to32767.- An integer literal is always an
int; convert it withint16(5). - Arithmetic on two
int16values gives anint16, and panics when the result leaves the range.
Basic examples
Convert an int or a whole float64:
int16(1200) // 1200
int16(-7.0) // -7
int16(32767) // 32767
int16(-32768) // -32768Two int16 values add to an int16:
let a = int16(1000)
let b = int16(234)
a + b // 1234Scope
Every numeric type converts, the sized ones included:
int16(byte(200)) // 200
int16(uint32(65)) // 65
int16(float32(-3.0)) // -3
int16(int8(-128)) // -128int16 is a function value, so it converts every element of an array:
[10, -20, 30].map(int16) // [10, -20, 30]It unwraps a named primitive type over a number:
type Celsius = int16
let freezing = Celsius(int16(0))
freezing // Celsius(0)
int16(freezing) // 0In a pipe, the value is the argument:
("-512".toInt() ?? 0) | int16() // -512Applications
Store an audio sample in 16 bits. Scale a level in -1.0 to 1.0, round it, and convert:
func toSample(level: float64): int16 {
int16(Math.round(Math.clamp(level, -1.0, 1.0) * 32767.0))
}
[0.0, 0.5, -1.0, 1.5].map(toSample) // [0, 16384, -32767, 32767]Widen to int before a sum that may pass 32767:
let samples = [int16(30000), int16(20000), int16(-1000)]
samples.map(int).sum() // 49000Properties and relations
Every int in range survives the round trip through int16:
for n in [-32768, -1, 0, 1, 32767] {
assert(int(int16(n)) == n)
}Converting a float with a fraction needs an explicit rounding first; Math.trunc rounds toward zero:
assert(int16(Math.trunc(2.7)) == int16(2))
assert(int16(Math.trunc(-2.7)) == int16(-2))
assert(int16(Math.round(2.5)) == int16(3))Bits.wrapAdd wraps around where + panics:
assert(Bits.wrapAdd(int16(30000), int16(30000)) == int16(-5536))Possible issues
A value outside the range panics; it does not wrap:
int16(32768) // panic: 32768 out of range for int16
int16(-32769) // panic: -32769 out of range for int16A fraction panics instead of truncating:
int16(2.5) // panic: 2.5 has a fraction, so it does not convert to int16NaN and the infinities panic:
int16(Math.NaN) // panic: NaN does not convert to int16
int16(Math.Infinity) // panic: Infinity does not convert to int16An integer literal is an int, not an int16:
let level: int16 = 5 // error: cannot use int where int16 is expectedConvert the literal:
let level: int16 = int16(5)
level // 5An int16 and an int do not mix in arithmetic:
let a = int16(1)
println(a + 1) // error: + needs both operands of one type, got int16 and intThe same holds for a comparison with a literal:
let a = int16(1)
println(a > 0) // error: > needs both operands of one type, got int16 and intConvert the literal instead:
let a = int16(1)
a > int16(0) // trueArithmetic on int16 panics when the result leaves the range, even when the final value would fit an int:
int16(30000) + int16(30000) // panic: integer overflow
int16(-32768) / int16(-1) // panic: integer overflowA sum of int16 values is an int16 too, so it overflows the same way:
[int16(30000), int16(20000)].sum() // panic: Array.sum: integer overflowWiden first with .map(int).sum(), as in Applications.
See also
int · int8 · int32 · int64 · uint16 · float64 · Math.trunc · Math.round · Math.clamp · Bits.wrapAdd · Arithmetic operators
