number
A number represents a numeric scalar value. Numbers support arithmetic, comparison, and conversion operations. Mech distinguishes numeric kinds by precision and representation, but they share common semantics. Numbers are divided into several categories: integers (signed and unsigned), floating-point, rational, and complex. Each type has its own range and precision characteristics, and are supported by various built-in operations and functions.
Numbers in Mech can be classified into the following categories:
Integers - Whole numbers, which can be positive, negative, or zero
Signed - Can represent both negative and positive values
i8- 8-bit signed integeri16- 16-bit signed integeri32- 32-bit signed integeri64- 64-bit signed integeri128- 128-bit signed integerUnsigned - Can only represent zero and positive values
Float - Decimal numbers with fractional components
Rational - Numbers represented as fractions of integers
r64- 64-bit rational numberComplex - Numbers with real and imaginary components
c64- 64-bit complex number
Numeric Types
Integer Numbers
Both signed and unsigned integers from 8 to 128 bits are supported in Mech. Signed integers can represent negative values, while unsigned integers can only represent zero and positive values.
Signed Integers
Type | Description | Minimum Value | Maximum Value |
|---|---|---|---|
|
|
| |
|
|
| |
|
|
| |
|
|
| |
|
|
|
Unsigned Integers
Type | Description | Minimum Value | Maximum Value |
|---|---|---|---|
|
|
| |
|
|
| |
|
|
| |
|
|
| |
|
|
|
For more, see integer.
Float Numbers
Float numbers in Mech are represented using standard floating-point types. Mech supports both f32 and f64 types, which correspond to 32-bit and 64-bit IEEE 754 floating-point numbers, respectively.
Type | Description | Precision | Range |
|---|---|---|---|
32-bit floating-point number | 7 decimal digits |
| |
64-bit floating-point number | 15 decimal digits |
|
For more, see float.
Rational Numbers
Rational numbers in Mech are represented using the r64 type, which allows for exact representation of fractions as ratios of two integers.
Type | Description | Examples |
|---|---|---|
64-bit rational number | 3/4, -5/2, 7/1 |
For more, see rational.
Complex Numbers
Complex numbers in Mech are represented using the c64 type, which consists of a real and an imaginary component, both represented as f64 floating-point numbers.
Type | Description | Examples |
|---|---|---|
64-bit complex number | 3+4i, 1+-2i, 0+1i |
For more, see complex.
Syntax
Numeric literals can be expressed in various formats:
Floating-point numbers, expressed in standard decimal notation and scientific notation
Integers, expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), and binary (base 2)
Rational numbers, expressed as fractions
a/b, whereais the numerator andbis the denominatorComplex numbers, expressed in the form
a + biora + bj, whereais the real part andbis the imaginary part
Floating-Point Literals
Floating-point literals are used to represent decimal numbers with fractional components. They are flexible in that they can represent both very large and very small numbers, and also can represent integer values between -1.7e308 and 1.7e308 for f64, and between -3.4e38 and 3.4e38 for f32.
Floating-point literals are expressed in standard decimal notation or scientific notation:
42--inferred as
3.14-0.0012.5e+3.-1.2e-2.f64
Each value is inferred as f64 unless otherwise specified. To specify a specific type, floating-point literals can also be suffixed with a specific kind:
42<f32>3.14<f32>-0.001<f64>2.5e+3.<f32>-1.2e-2.<f64>
Integer Literals
Integer literals can be written in the following formats:
0d42--decimal
0x2ABC--hexadecimal
0o654--octal
0b101010--binary
Decimals without a prefix are inferred as floating-point numbers (f64), while those with the 0d prefix are inferred as signed integers (i64). Each of the other formats are also inferred as i64 unless otherwise specified. Any integer literal can be suffixed with a specific kind to indicate its type:
42u8-- unsigned 8-bit integer
Decimal literals can be signed or unsigned. Signed literal types include i8, i16, i32, i64, and i128. Unsigned literal types include u8, u16, u32, u64, and u128.
Rational Literals
Rational numbers are used to represent fractions exactly as ratios of two integers. They are expressed in the form a/b, where a is the numerator and b is the denominator.
Rational numbers are written as follows:
3/4-5/27/7--A whole number represented as a rational
Two rational literals can represent the same numeric value but have different representations:
Represents 0.5
Also represents 0.5
Evaluates to true
Complex Literals
Complex numbers are used to represent numbers with both real and imaginary components. They are expressed in the form a + bi or a + bj, where a is the real part and b is the imaginary part.
Complex numbers are written as follows:
3+4i1+-2i0+1i
The imaginary unit can be represented by either i or j. Both representations are equivalent and can be used interchangeably.