Browse the documentation

Preview documentation for AEL Beta 0.0.1 — launching soon

Numbers

AEL, the Agent Engineering Language, will have eight fixed-width integer types, and every result of arithmetic on them will be defined: an overflow or an out-of-range value will be reported, never silently ignored. This page is the reference for the integer types, their operators and the number operations of the standard library.

Status

Planned for AEL Beta 0.0.1. AEL is not available yet.

Package names on this page are preview naming and may change before launch.

Integer types

TypeWidthSmallest valueLargest value
i88 bits−128127
i1616 bits−32,76832,767
i3232 bits−2,147,483,6482,147,483,647
i6464 bits−9,223,372,036,854,775,8089,223,372,036,854,775,807
u88 bits0255
u1616 bits065,535
u3232 bits04,294,967,295
u6464 bits018,446,744,073,709,551,615
  • Signed types will use two's complement, the same representation on every target.
  • Each width and sign will be its own type. An i32 will never be turned into an i64, or a u8 into a u16, without an explicit conversion.
  • bool will not be a number, and a number will never be a condition.

Literals

  • Integer literals will be decimal: 0, 42, 1_000_000. A single _ may separate digits.
  • A literal will take the type its place expects: in let port: u16 = 8080; the literal will be a u16. When nothing says otherwise, it will be an i64.
  • A literal will have to fit the type it takes. let small: u8 = 300; will be refused when you check your code, not when it runs.
  • A minus sign will be an operator. The smallest signed value will still be writable directly: -128 as an i8.

Arithmetic

The same rules will apply in every build, with no difference between a debug build and a release build.

OperatorResultWill stop the program when
a + b, a - b, a * bThe exact result.The result does not fit the type.
a / bThe quotient, rounded toward zero: -7 / 2 is -3.b is zero, or a signed type's smallest value is divided by -1.
a % bThe remainder, with the sign of a: -7 % 2 is -1.b is zero, or a signed type's smallest value is divided by -1.
-aThe negated value. Signed types only.a is its type's smallest value.

Both sides of an operator will have to have the same type. Stopping the program will be a panic: Errors and checked arithmetic describes what a panic does, and Results and errors how to report a failure your code expects instead.

Comparison and bits

  • ==, !=, <, <=, > and >= will compare two integers of the same type and give a bool. Signed types will compare as signed values, unsigned types as unsigned ones. Comparisons will not chain: write a < b && b < c.
  • &, |, ^ and ~ will work on the exact bits of one integer type.
  • The shift count of << and >> will be an unsigned integer of the same width as the value, and will have to be less than that width: a larger count will stop the program. << will stop the program when the result does not fit; >> will round a signed value down and fill an unsigned one with zeros.

Wrapping and saturating operations

When you want a result other than a stop, you will ask for it by name, at the place where you need it. The meaning of + will never change.

OperationsOn a result that does not fitTypes
Wrapping addition, subtraction, multiplication and negationThe result will wrap around the type's range, as if counted modulo 2 to the power of the width.Every integer type.
Saturating addition, subtraction and multiplicationThe result will stay at the type's smallest or largest value.Every integer type.
Saturating negationThe smallest value will become the largest.Signed types.

The preview library has no wrapping or saturating division.

Checked conversions

Converting between integer types will always be explicit. The standard library will have one conversion for each of the eight target types. Each will take an integer of any type and return Result<T, CastError>, where T is the target type:

  • Result::Ok(value) will hold the same number in the new type;
  • Result::Err(CastError::OutOfRange) will mean the number does not fit, for example 300 into a u8 or -1 into a u64.

No conversion will silently cut off bits or change a sign.

An example

Checked arithmetic will stop the program on an overflow. When a large value is something your program expects, test for it first and return an error instead:

Preview syntax — may change before launch
fn add_stock(on_hand: u16, delivered: u16) -> Result<u16, CapacityError> {
    if delivered > 65_535 - on_hand {
        return Result::Err(CapacityError::Full);
    }
    return Result::Ok(on_hand + delivered);
}

Not in the preview

These are not part of the preview syntax. Their rules will be described here when the syntax is final:

  • Floating-point and fixed-point numbers, and the machine-sized integers usize and isize.
  • Hexadecimal, binary and exponent literals.

Numerical arrays, complex numbers and linear algebra will come from optional official packages, such as math/array and math/linalg. Scientific computing describes the scientific packages.