Documentation for syntax Number literals
assembled from the following pages:
Language documentation: Syntax §
From Syntax
(Syntax) Syntax Number literals Number literals §
Number literals are generally specified in base ten (which can be specified literally, if needed, via the prefix 0d
), unless a prefix like 0x
(hexadecimal, base 16), 0o
(octal, base 8) or 0b
(binary, base 2) or an explicit base in adverbial notation like :16<A0>
specifies it otherwise. Unlike other programming languages, leading zeros do not indicate base 8; instead a compile-time warning is issued.
In all literal formats, you can use underscores to group digits, although they don't carry any semantic information; the following literals all evaluate to the same number:
1000000 1_000_000 10_00000 100_00_00
Int
literals
§
Integers default to signed base-10, but you can use other bases. For details, see Int.
# not a single literal, but unary - operator applied to numeric literal 2 -2 12345 0xBEEF # base 16 0o755 # base 8 :3<1201> # arbitrary base, here base 3
Rat
literals
§
Rat literals (rationals) are very common, and take the place of decimals or floats in many other languages. Integer division also results in a Rat
.
1. # Error: A number must have at least one digit after the radix point 1.0 3.14159 -2.5 # Not actually a literal, but still a Rat :3<21.0012> # Base 3 rational ⅔ 2/3 # Not actually a literal, but still a Rat
Num
literals
§
Scientific notation with an integer exponent to base ten after an e
produces floating point number:
1.e0 # error: A number must have at least one digit after the radix point 1e0 6.022e23 1e-9 -2e48 2e2.5 # error
Complex
literals
§
Complex numbers are written either as an imaginary number (which is just a rational number with postfix i
appended), or as a sum of a real and an imaginary number:
1.+2i # error: A number must have at least one digit after the radix point 1+2.i # error: A number must have at least one digit after the radix point 1+2i 6.123e5i # note that this is 6.123e5 * i, not 6.123 * 10 ** (5i)