LEVIATHAN vd52e79c4 · d52e79c486

Lexical

Numeric literals

Decimal, hexadecimal and binary integers, floats, the m suffix of a decimal literal, and the _ digit separator.

since 0.1.0-alpha.1linuxwindowswasm

Description

An integer literal is written in decimal, hexadecimal (0x or 0X) or binary (0b or 0B). There is no octal form: 010 is the decimal number ten. A literal with a fractional part, such as 3.14, is a float.

An underscore may separate digits for readability in any base, including in the fractional digits of a float. It does not change the value.

A base-10 literal followed directly by m, such as 19.99m or 5m, is a decimal literal (lang.decimal): it is a decimal in every context, built exactly from its digits with the scale it was written with.

Integer and float literals

use io::console;

console.writeln(42);
console.writeln(0xFF);
console.writeln(0B1010);
console.writeln(1_000_000);
console.writeln(0x1_FF);
console.writeln(3.14);
console.writeln(1_000.000_5);
console.writeln(010);
console.writeln(0x8000000000000000 == 1 << 63);
Range r = 0xA..0xC;
for (int i in r) {
    console.writeln(i);
}
42
255
10
1000000
511
3.14e0
1.0000005e3
10
true
10
11
12

Rules

  • A literal is decimal, 0x/0X hexadecimal or 0b/0B binary. There is no octal.
  • _ may separate digits in any base. It may not lead, trail or double up: 0x_1F, 1000_ and 1__000 are compile errors.
  • A hexadecimal or binary literal is always an int. 0x1.5 is a compile error, not a fraction. 0xA..0xF is the range operator applied to two hexadecimal integers.
  • A hexadecimal or binary literal uses its full 64-bit pattern, so 0x8000000000000000 is the same value as 1 << 63, which is the smallest int.
  • A float literal needs digits on both sides of the point. It may end in an exponent: a lowercase e, an optional - and one or more digits, with no + and no _ (1.5e3, 5.0e-324). This is exactly the form a float prints in, so every printed float is valid source. The point is still required: 1e3 is an int followed by a name and is a compile error, and so are 1.0E3 and 1.0e+3. An e not followed by a digit (or by - and a digit) is not part of the number, so 1.0else is still 1.0 then else.
  • A float literal is rounded once, from its digits straight to the nearest value of its type: a float32, float16 or float8 literal is not rounded to a float first, so the text a narrow float prints reads back as the same value. A float literal too large for a float is a compile error in every float context, literal 1.0e999 out of range for 'float' (max finite 1.7976931348623157e308) (a sized float names its own bound); one too small to represent is zero (1.0e-999 is 0.0e0).
  • A decimal literal has no exponent: 1.0e5m is a compile error, a decimal literal has no exponent. A float literal with an exponent in a decimal context becomes the decimal that "<literal>".toDecimal() gives, with the same value and scale (_ separators ignored): decimal d = 1.0e5; is 100000 with scale 0 and 1.5e-3 is 0.0015 with scale 4. One that toDecimal() cannot represent is the not-exactly-representable error below.
  • A literal has type int or float unless the context asks for a sized numeric type, in which case it is converted and range-checked at compile time (see lang.sized-integers and lang.sized-floats).
  • The index of a Map is such a context: its key type asks, and T? asks for T. So is the index of an indexer whose receiver has no indexer that takes the literal as it is.
  • In a decimal context an integer or float literal becomes a decimal exactly from its digits (a hexadecimal or binary literal is the unsigned value of its digits, so 0xFFFFFFFFFFFFFFFF is 18446744073709551615); its scale is the number of fraction digits written, _ separators ignored (decimal d = 1_000.50; has scale 2). A literal that a decimal cannot hold exactly (more than 34 significant digits, a nonzero digit after the 34th place, or a magnitude of 10^34 or more) is a compile error, literal <text> is not exactly representable as 'decimal' (34 significant digits, scale 0..34). A trailing zero beyond the 34th place is dropped.
  • The m suffix makes a base-10 literal a decimal in every context; int i = 1m; is a conversion error, not a range error. It must follow the last digit directly and must not be followed by a letter, digit or _: 1.5max is the float 1.5 and the name max. m after a hexadecimal or binary literal is a compile error, the decimal suffix 'm' needs a base-10 literal.

Examples

An exponent literal is the printed form of a float, so printed text pastes back as the same value:

Exponent literals

use io::console;

console.writeln(1.5e3);
console.writeln(2.5e-7 * 2.0);
console.writeln(1.0e300 == 10.0.pow(300.0));
float x = 3.0000000000000004e-1;
console.writeln(x == 0.1 + 0.2);
console.writeln(x.toString().toFloat() == x);
1.5e3
5.0e-7
true
true
true

Digit separators in each base, and a hex pattern:

Separators and bases

use io::console;

console.writeln(0b1111_0000);
console.writeln(0xDEAD_BEEF);
console.writeln(0xFF == 255);
console.writeln(1_000 * 3);
240
3735928559
true
3000

Decimal literals keep their scale and are exact:

Decimal literals

use io::console;

console.writeln(19.99m);
console.writeln(2.50m);
console.writeln(1_000.000_5m);
console.writeln(0.1m + 0.2m);
decimal big = 123456789012345678901234567890;
console.writeln(big);
decimal ratio = 5 / 100;
console.writeln(ratio);
19.99
2.50
1000.0005
0.3
123456789012345678901234567890
0.05

Notes

An expression made only of literals takes the type of its context, like a single literal, and is evaluated at compile time in that type. float f = 7 / 2; is 3.5, because both literals become floats. With no context, integer literals stay int, so var x = 7 / 2; is 3. Make one operand a float literal to get a float without a context: 7 / 2.0 is 3.500000. An m literal does the same for decimal: 7 / 2m is 3.5. See lang.numeric-conversions.

See also

  • Literals at a glance — Every literal form of the language and the type each one has.
  • decimal — The exact base-10 number for money and other decimal quantities — 34 significant digits, a kept scale, value equality and half-even rounding.
  • Numeric conversions and working types — Values cross between integer and float types only through a conversion call, lossless widening is implicit, and literals take the type their context asks for.