LEVIATHAN v962456e · 962456eee1

Standard Library

class int16

A signed 16-bit integer: an int16 holds -32768 to 32767 and is stored unboxed.

since 0.1.0-alpha.1linuxwindowswasm

Overview

Arithmetic between int16 values wraps around in two's complement at 16 bits instead of failing: 32767 + 1 is -32768 and -32768 - 1 is 32767. Even -32768 / -1, whose true result 32768 does not fit, gives -32768. >> keeps the sign. A bare integer literal next to an int16 operand counts as an int16, while an int variable makes the result an int. Division or remainder by zero throws a RuntimeException. A literal assigned to an int16 is checked when the program is compiled.

Converting an int with int.toInt16() does not wrap, it throws when the value is outside -32768..32767. toInt() converts back and never fails.

Examples

Wrapping and range

int16 top = 32767;
int16 one = 1;
console.writeln(top + one);
int16 bottom = -32768;
console.writeln(bottom - one);
console.writeln(bottom.abs());
int16 big = 30000;
console.writeln(big + big);
try {
    console.writeln(40000.toInt16());
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
-32768
32767
-32768
-5536
caught: toInt16: value out of range (-32768..32767)

Methods

abs

@__narrowShared("abs")
abs() -> int16

Return the absolute value.

Two's complement has one more negative value than positive ones, so the absolute value of -32768, the smallest int16, does not fit and abs returns -32768 unchanged.

Returns

this when it is zero or positive, otherwise -this, as a int16.

Examples

int16 n = -300;
console.writeln(n.abs());
int16 bottom = -32768;
console.writeln(bottom.abs());
int16 top = 32767;
console.writeln(top.abs());
300
-32768
32767

clamp

@__narrowShared("clamp")
clamp(int16 lo, int16 hi) -> int16

Limit the int16 to the range from lo to hi.

Parameters

lo
The smallest value to return.
hi
The largest value to return.

Returns

lo if this is below it, hi if this is above it, otherwise this.

Throws

RuntimeException
when lo is greater than hi.

Examples

int16 lo = -1000;
int16 hi = 1000;
int16 top = 32767;
int16 bottom = -32768;
int16 mid = 300;
console.writeln(top.clamp(lo, hi));
console.writeln(bottom.clamp(lo, hi));
console.writeln(mid.clamp(lo, hi));
try {
    console.writeln(mid.clamp(hi, lo));
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
1000
-1000
300
caught: clamp: lo > hi

max

@__narrowShared("max")
max(int16 other) -> int16

Return the larger of two int16 values.

Parameters

other
The value to compare with.

Returns

this if it is greater than other, otherwise other.

Examples

int16 top = 32767;
int16 bottom = -32768;
console.writeln(top.max(bottom));
console.writeln(bottom.max(bottom));
32767
-32768

See also: min

min

@__narrowShared("min")
min(int16 other) -> int16

Return the smaller of two int16 values.

Parameters

other
The value to compare with.

Returns

this if it is less than other, otherwise other.

Examples

int16 top = 32767;
int16 bottom = -32768;
console.writeln(top.min(bottom));
console.writeln(top.min(top));
-32768
32767

See also: max

pow

@__narrowShared("pow")
pow(int16 e) -> int16

Raise the int16 to a power, wrapping at 16 bits.

The multiplication is repeated squaring and wraps at 16 bits at every step, so the result is the true power folded into -32768..32767: 3 to the 10th is -6487 (59049 minus 65536) and 2 to the 15th is -32768. A negative exponent has no integer result and gives 0, as with int.pow.

Parameters

e
The exponent.

Returns

this to the power e, as a int16.

Examples

int16 three = 3;
int16 ten = 10;
int16 eleven = 11;
console.writeln(three.pow(ten));
console.writeln(three.pow(eleven));
int16 two = 2;
int16 fifteen = 15;
console.writeln(two.pow(fifteen));
int16 minusOne = -1;
console.writeln(two.pow(minusOne));
-6487
-19461
-32768
0

sign

@__narrowShared("sign")
sign() -> int16

Return the sign of the value.

Returns

-1 if this is negative, 0 if it is zero, and 1 if it is positive, as a int16.

Examples

int16 bottom = -32768;
int16 zero = 0;
int16 top = 32767;
console.writeln(bottom.sign());
console.writeln(zero.sign());
console.writeln(top.sign());
-1
0
1

toFloat

toFloat() -> float

Convert the int16 to a float.

The conversion is exact: every value of a int16 is representable as a float.

Returns

The value as a float.

Examples

int16 top = 32767;
console.writeln(top.toFloat());
console.writeln(top.toFloat() / 2.0);
32767.000000
16383.500000

toHex

toHex() -> string

Format the int16 in hexadecimal.

The digits are lowercase with no 0x prefix and no padding. A negative value gets a leading - before the digits of its magnitude, so -32768 formats as -8000 and -1 as -1.

Returns

The hexadecimal digits of this.

Examples

int16 top = 32767;
console.writeln(top.toHex());
int16 bottom = -32768;
console.writeln(bottom.toHex());
int16 minusOne = -1;
console.writeln(minusOne.toHex());
7fff
-8000
-1

See also: toString

toInt

toInt() -> int

Convert the int16 to an int.

The value is unchanged; every int16 value fits in an int, so this conversion never throws. Convert back with int.toInt16, which throws when the value is outside -32768..32767.

Returns

The same number as an int.

Examples

int16 m = -32768;
console.writeln(m.toInt() - 1);
console.writeln(m - 1);
-32769
32767

See also: toInt16

toString

toString() -> string

Format the int16 as decimal text.

A negative value gets a leading -. The toString(int radix) overload formats in another base.

Returns

The decimal digits of this.

Examples

int16 n = -32768;
console.writeln(n.toString());
console.writeln("n = " + n.toString());
console.writeln(n.toString(2));
console.writeln(n.toString(16));
-32768
n = -32768
-1000000000000000
-8000
toString(int radix) -> string

Format the int16 as text in the given base.

Digits above 9 are lowercase letters (a is ten). A negative value gets a leading - before the digits of its magnitude. This formats the same number as toInt().toString(radix).

Parameters

radix
The base, from 2 to 36 inclusive.

Returns

The digits of this in base radix.

Throws

RuntimeException
when radix is outside 2..36.

See also: toHex