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/0Xhexadecimal or0b/0Bbinary. There is no octal. _may separate digits in any base. It may not lead, trail or double up:0x_1F,1000_and1__000are compile errors.- A hexadecimal or binary literal is always an
int.0x1.5is a compile error, not a fraction.0xA..0xFis the range operator applied to two hexadecimal integers. - A hexadecimal or binary literal uses its full 64-bit pattern, so
0x8000000000000000is the same value as1 << 63, which is the smallestint. - 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 afloatprints in, so every printed float is valid source. The point is still required:1e3is anintfollowed by a name and is a compile error, and so are1.0E3and1.0e+3. Anenot followed by a digit (or by-and a digit) is not part of the number, so1.0elseis still1.0thenelse. - A float literal is rounded once, from its digits straight to the nearest value of its type: a
float32,float16orfloat8literal is not rounded to afloatfirst, so the text a narrow float prints reads back as the same value. A float literal too large for afloatis 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-999is0.0e0). - A decimal literal has no exponent:
1.0e5mis a compile error,a decimal literal has no exponent. A float literal with an exponent in adecimalcontext becomes thedecimalthat"<literal>".toDecimal()gives, with the same value and scale (_separators ignored):decimal d = 1.0e5;is100000with scale 0 and1.5e-3is0.0015with scale 4. One thattoDecimal()cannot represent is the not-exactly-representable error below. - A literal has type
intorfloatunless the context asks for a sized numeric type, in which case it is converted and range-checked at compile time (seelang.sized-integersandlang.sized-floats). - The index of a
Mapis such a context: its key type asks, andT?asks forT. So is the index of an indexer whose receiver has no indexer that takes the literal as it is. - In a
decimalcontext an integer or float literal becomes adecimalexactly from its digits (a hexadecimal or binary literal is the unsigned value of its digits, so0xFFFFFFFFFFFFFFFFis18446744073709551615); its scale is the number of fraction digits written,_separators ignored (decimal d = 1_000.50;has scale 2). A literal that adecimalcannot 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
msuffix makes a base-10 literal adecimalin 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.5maxis the float1.5and the namemax.mafter 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.