int8
func int8[T: Number](x: T): int8int8 converts x to int8 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; int8(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
int8holds whole numbers from -128 to 127.- An integer literal is always an
int, so anint8value always starts from a conversion such asint8(5). - Arithmetic on two
int8values gives anint8and panics when the result leaves the range; Bits.wrapAdd, Bits.wrapSub, and Bits.wrapMul wrap instead.
Basic examples
int8(42) // 42
int8(-128) // -128
int8(127) // 127A float converts when it is a whole number:
int8(3.0) // 3
int8(-0.0) // 0A value outside the range panics:
int8(128) // panic: 128 out of range for int8
int8(-129) // panic: -129 out of range for int8Scope
Any numeric type converts, as long as the value fits:
int8(byte(100)) // 100
int8(uint64(7)) // 7
int8(int16(-50)) // -50
int8(float32(2.0)) // 2
int8(byte(200)) // panic: 200 out of range for int8A float with a fraction, NaN, and the infinities panic:
int8(2.5) // panic: 2.5 has a fraction, so it does not convert to int8
int8(Math.NaN) // panic: NaN does not convert to int8
int8(Math.Infinity) // panic: Infinity does not convert to int8A named primitive over a number is unwrapped first:
type Celsius = int
let today = Celsius(21)
int8(today) // 21int8 is a function value, so it can be passed where a function is expected:
[1, -2, 3].map(int8) // [1, -2, 3]In a pipe, the value is the argument:
100.0 | int8() // 100Applications
Read a signed byte. A Bytes element is a byte from 0 to 255; values from 128 up stand for negative numbers in two's complement:
func signed(b: byte): int8 {
if b < byte(128) { int8(b) } else { int8(int(b) - 256) }
}
let data = Bytes.fromArray([0x05, 0xff, 0x80, 0x7f])
for b in data {
println(signed(b))
}5
-1
-128
127Clamp a value into the range before converting it, so that the conversion cannot panic:
func saturate(n: int): int8 {
int8(Math.clamp(n, -128, 127))
}
println(saturate(300))
println(saturate(-1000))
println(saturate(12))127
-128
12Truncate or round a measurement first, then convert:
let reading = -12.8
println(int8(Math.trunc(reading)))
println(int8(Math.round(reading)))-12
-13Properties and relations
Every int from -128 to 127 survives the round trip through int8:
for n in Iter.range(-128, 128) {
assert(int(int8(n)) == n)
}Converting a whole float gives the same int8 as converting the matching int:
for n in [-128, -1, 0, 64, 127] {
assert(int8(float64(n)) == int8(n))
}Bits.wrapAdd agrees with + whenever + does not overflow:
let a = int8(100)
let b = int8(27)
assert(Bits.wrapAdd(a, b) == a + b)Possible issues
An integer literal is an int, never an int8, so it does not fit an int8 slot. Convert it:
let level: int8 = 5 // error: cannot use int where int8 is expectedlet level: int8 = int8(5)
level // 5An array literal of integers is an Array[int], so it does not fit an Array[int8] slot:
let xs: Array[int8] = [1, 2, 3] // error: cannot use int where int8 is expectedConvert each element instead:
let xs = [1, 2, 3].map(int8)
xs // [1, 2, 3]A hex literal is an int too, so 0xff is 255, not -1, and does not fit. To read a byte as a signed number, see the signed-byte example under Applications:
int8(0xff) // panic: 255 out of range for int8int8 and int do not mix in arithmetic. Convert one side so that both have the same type:
let step = int8(3)
let next = step + 1 // error: + needs both operands of one type, got int8 and intlet step = int8(3)
step + int8(1) // 4
int(step) + 1 // 4Arithmetic overflows quickly in eight bits, and overflow panics:
let a = int8(100)
a + a // panic: integer overflow
Bits.wrapAdd(a, a) // -56The range is not symmetric: -128 has no positive counterpart, so negating it overflows:
let low = int8(-128)
-low // panic: integer overflow
low / int8(-1) // panic: integer overflowSee also
int16 · int · uint8 · byte · float64 · Math.trunc · Math.clamp · Bits.wrapAdd · Numbers · Arithmetic
