Integers
Eight sized integer types with fixed ranges on every platform, an unannotated literal is always Int64, and overflow is a compile error when it is written and a panic when it happens at runtime.
Diese Seite gibt es noch nicht auf Deutsch, deshalb steht sie hier auf Englisch. Zur englischen Seite
An integer type always carries its width: Int8 through Int64 signed, UInt8 through UInt64 unsigned. There is
no platform-dependent "the native integer size" - the same program overflows at the same value on every platform.
Example#
const count: Int = 5
const byte: UInt8 = 200
const smallest: Int8 = Int8.minimum
const largest: Int8 = Int8.maximum
print "{count} {byte} {smallest} {largest}"Syntax#
Int8 Int16 Int32 Int64 signed, in increasing width
UInt8 UInt16 UInt32 UInt64 unsigned, in increasing width
Int UInt aliases for the 64-bit width
Type.minimum Type.maximum the smallest and largest value of a typeRules#
Intis an alias forInt64, andUIntis an alias forUInt64. WriteIntfor "a number" and the sized name when the width itself is the point - a binary format, FFI, memory layout.An integer literal without an expected type is
Int64. This is fixed and does not follow theIntalias, so a file that shadowsIntstill hasInt64literals. See Literals.Every integer type carries
minimumandmaximumas its own constants, so a bound is named instead of repeated:Int8.minimumis-128,Int8.maximumis127,UInt64.maximumis18446744073709551615.A literal that does not fit its target type is a compile error at the literal, not at some later use. It is read in its target type and never as an
Int64first, so aUInt64takes every literal up to18446744073709551615, the upper half anInt64does not have included.Kompiliert nichtconst small: Int8 = 300 // error: `300` does not fit into `Int8`const basis: UInt64 = 14695981039346656037 print basisOverflow at runtime panics; overflow written directly in the source is caught where it is written. Rule 4 is the compile-time half of this rule; the runtime half is the same panic every other bug produces (see what a panic does).
Integer division truncates toward zero, and the remainder takes the sign of the dividend.
-7 / 2is-3,-7 % 2is-1, soais always(a / b) * b + a % b. Dividing by zero panics, and so does dividing the smallest value of a signed type by-1- an overflow like any other.There are no implicit numeric conversions. Converting between two integer types, or between an integer and a
Float, always goes throughFrom,TryFromor.into().Kompiliert nichtconst whole: Int64 = 5 const asFloat: Float = whole // error: Expected `Float64`, found `Int64`const whole: Int64 = 5 const asFloat = Float.from whole print asFloatA widening conversion is
Fromand never fails; a narrowing one isTryFromand returns aResult. Every signed type widens into a larger signed type, and every unsigned type up toUInt32widens intoInt64. Narrowing (Int64intoInt8,Int64intoInt32,Float64intoInt64) isTryFrom<..., NumberRangeError>.const big: Int64 = 300 const narrowed = Int8.tryFrom big match narrowed { Ok(value) => print value Fail(problem) => print problem }&,|,^,~,<<and>>are the bit operators of every integer type, and the members ofBits.&binds like*,|and^like+, a shift between*and**, soflags & 1 == 0is(flags & 1) == 0. A shift of a signed type is arithmetic and one of an unsigned type logical, the bits that leave the width are dropped, and a shift by a negative amount or by the width of the type or more panics.const flags: UInt8 = 0xFF const masked = flags & 0x0F const shifted = flags << 2 print "{masked} {shifted} {~masked}" // prints 15 252 240Every operator that can panic has a checked twin that answers an
Option.addedChecked,subtractedChecked,multipliedChecked,dividedCheckedandremainderCheckedbelong to every integer type, through the traitInteger, and answerNoneexactly where+,-,*,/and%panic - an overflow, a zero divisor, the smallest value of a signed type divided by-1. The operator says "this fits"; the twin is for a value that comes from outside and may not.const stock: Int8 = 100 print stock.addedChecked(27) print stock.addedChecked(28) print 7.dividedChecked(0) // prints Some(127) // prints None // prints NoneUInt64alone hasaddedWrappingandmultipliedWrapping, the only arithmetic that does not panic on overflow. They exist for hash functions, which need the wrap; every other numeric type panics on overflow the way+always does.
What this is not#
There is no C trap. In C, x & 1 == 0 is x & (1 == 0); here & binds like *, so it is the test it looks like.
const flags = 0b1010
print(flags & 1 == 0)
// prints true^ is exclusive or, not a power. A power is **, and a float, which has no bits to combine, says so:
const wrong = 2.0 ^ 2.0
print wrong
// error: `Float64` has no `^`: it is the exclusive or of two integersRelated#
- Built-in types - the numeric types in the context of every other built-in type.
- Floating-point numbers -
Float32,Float64, and why==andcomparedisagree. - Literals - the four ways to write an integer literal.