You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reader.rst
+54-4Lines changed: 54 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,8 @@ Numeric Literals
21
21
22
22
The Basilisp reader reads a wide range of numeric literals.
23
23
24
+
.. _integer_numbers:
25
+
24
26
Integers
25
27
^^^^^^^^
26
28
@@ -35,11 +37,22 @@ Integers
35
37
basilisp.user=> (python/type 1N)
36
38
<class 'int'>
37
39
38
-
Integers are represented using numeric ``0-9`` and may be prefixed with any number of negative signs ``-``.
39
-
The resulting integer will have the correct sign after resolving all of the supplied ``-`` signs.
40
+
Integers are represented using numeric ``0-9`` and may be prefixed with a single negative sign ``-``.
40
41
For interoperability support with Clojure, Basilisp integers may also be declared with the ``N`` suffix, like ``1N``.
41
42
In Clojure, this syntax signals a ``BigInteger``, but Python's default ``int`` type supports arbitrary precision by default so there is no difference between ``1`` and ``1N`` in Basilisp.
42
43
44
+
Integer literals may be specified in arbitrary bases between 2 and 36 by using the syntax ``[base]r[value]``.
45
+
For example, in base 2 ``2r1001``, base 12 ``12r918a32``, and base 36 ``36r81jdk3kdp``.
46
+
Arbitrary base literals do not distinguish between upper and lower case characters, so ``p`` and ``P`` are the same for bases which support ``P`` as a digit.
47
+
Arbitrary base literals do not support the ``N`` suffix because ``N`` is a valid digit for some bases.
48
+
49
+
For common bases such as octal and hex, there is a custom syntax.
50
+
Octal literals can be specified with a ``0`` prefix; for example, the octal literal ``0777`` corresponds to the base 10 integer 511.
51
+
Hex literals can be specified with a ``0x`` prefix; for example, the hex literal ``0xFACE`` corresponds to the base 10 integer 64206.
52
+
Both octal and hex literals support the ``N`` suffix and it is treated the same as with base 10 integers.
53
+
54
+
.. _floating_point_numbers:
55
+
43
56
Floating Point
44
57
^^^^^^^^^^^^^^
45
58
@@ -55,11 +68,30 @@ Floating Point
55
68
<class 'decimal.Decimal'>
56
69
57
70
Floating point values are represented using ``0-9`` and a trailing decimal value, separated by a ``.`` character.
58
-
Like integers, floating point values may be prefixed with an arbitrary number of negative signs ``-`` and the final read value will have the correct sign after resolving the negations.
71
+
Like integers, floating point values may be prefixed with a single negative sign ``-``.
59
72
By default floating point values are represented by Python's ``float`` type, which does **not** support arbitrary precision by default.
60
73
Like in Clojure, floating point literals may be specified with a single ``M`` suffix to specify an arbitrary-precision floating point value.
61
74
In Basilisp, a floating point number declared with a trailing ``M`` will return Python's `Decimal <https://docs.python.org/3/library/decimal.html>`_ type, which supports arbitrary floating point arithmetic.
62
75
76
+
.. _scientific_notation:
77
+
78
+
Scientific Notation
79
+
^^^^^^^^^^^^^^^^^^^
80
+
81
+
::
82
+
83
+
basilisp.user=> 2e6
84
+
2000000
85
+
basilisp.user=> 3.14e-1
86
+
0.31400000000000006
87
+
88
+
Basilisp supports scientific notation using the ``e`` syntax common to many programming languages.
89
+
The significand (the number to the left of the ``e`` ) may be an integer or floating point and may be prefixed with a single negative sign ``-``.
90
+
The exponent (the number to the right of the ``e`` ) must be an integer and may be prefixed with a single negative sign ``-``.
91
+
The resulting value will be either an integer or float depending on the type of the significand.
92
+
93
+
.. _complex_numbers:
94
+
63
95
Complex
64
96
^^^^^^^
65
97
@@ -76,7 +108,24 @@ Complex
76
108
77
109
Basilisp includes support for complex literals to match the Python VM hosts it.
78
110
Complex literals may be specified as integer or floating point values with a ``J`` suffix.
79
-
Like integers and floats, complex values may be prefixed with an arbitrary number of negative signs ``-`` and the final read value will have the correct sign after resolving the negations.
111
+
Like integers and floats, complex values may be prefixed with a single negative sign ``-``.
112
+
113
+
.. _ratios:
114
+
115
+
Ratios
116
+
^^^^^^
117
+
118
+
::
119
+
120
+
basilisp.user=> 22/7
121
+
22/7
122
+
basilisp.user=> -3/8
123
+
-3/8
124
+
125
+
Basilisp includes support for ratios.
126
+
Ratios are represented as the division of 2 integers which cannot be reduced to an integer.
127
+
As with integers and floats, the numerator of a ratio may be prefixed with a single negative sign ``-`` -- a negative sign may not appear in the denominator.
128
+
In Basilisp, ratios are backed by Python's `Fraction <https://docs.python.org/3/library/fractions.html>`_ type, which is highly interoperable with other Python numeric types.
80
129
81
130
.. _strings:
82
131
@@ -101,6 +150,7 @@ String literals are always read with the UTF-8 encoding.
101
150
String literals may contain the following escape sequences: ``\\``, ``\a``, ``\b``, ``\f``, ``\n``, ``\r``, ``\t``, ``\v``.
102
151
Their meanings match the equivalent escape sequences supported in `Python string literals <https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals>`_\.
0 commit comments