syntax Number literals
1 | language documentation Syntax |
1.1 | (Syntax) Number literals |
1.1.0.1 | Int literals |
1.1.0.2 | Rat literals |
1.1.0.3 | Num literals |
1.1.0.4 | Complex literals |
Documentation for syntax Number literals
assembled from the following types:
language documentation Syntax
From Syntax
(Syntax) 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:
10000001_000_00010_00000100_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-2123450xBEEF # base 160o755 # 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.03.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:
1e06.022e231e-9-2e482e2.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+2i6.123e5i # note that this is 6.123e5 * i, not 6.123 * 10 ** (5i)