Skip to content

Commit 4916def

Browse files
committed
Reword float literals
1 parent ad9ab63 commit 4916def

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

Doc/reference/datamodel.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Booleans (:class:`bool`)
262262
a string, the strings ``"False"`` or ``"True"`` are returned, respectively.
263263

264264

265+
.. _datamodel-float:
266+
265267
:class:`numbers.Real` (:class:`float`)
266268
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267269

Doc/reference/lexical_analysis.rst

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,12 @@ three types: integers, floating-point numbers, and imaginary numbers.
930930

931931
NUMBER: `integer` | `floatnumber` | `imagnumber`
932932

933+
The numeric value of a numeric literal is the same as if it were passed as a
934+
string to the :class:`int`, :class:`float` or :class:`complex` class
935+
constructor, respectively.
936+
Note that not all valid inputs for those constructors are also valid literals.
933937

934-
Note that numeric literals do not include a sign; a phrase like ``-1`` is
938+
Numeric literals do not include a sign; a phrase like ``-1`` is
935939
actually an expression composed of the unary operator '``-``' and the literal
936940
``1``.
937941

@@ -950,7 +954,7 @@ and the :ref:`imaginary literal <imaginary>` ``2j``.
950954
.. _integers:
951955

952956
Integer literals
953-
----------------
957+
^^^^^^^^^^^^^^^^
954958

955959
Integer literals denote whole numbers. For example::
956960

@@ -1023,26 +1027,58 @@ Formally, integer literals are described by the following lexical definitions:
10231027
.. _floating:
10241028

10251029
Floating-point literals
1026-
-----------------------
1030+
^^^^^^^^^^^^^^^^^^^^^^^
10271031

1028-
Floating-point literals are described by the following lexical definitions:
1032+
Floating-point (float) literals, such as ``3.14`` or ``1.5``, denote
1033+
:ref:`approximations of real numbers <datamodel-float>`.
10291034

1030-
.. productionlist:: python-grammar
1031-
floatnumber: `pointfloat` | `exponentfloat`
1032-
pointfloat: [`digitpart`] `fraction` | `digitpart` "."
1033-
exponentfloat: (`digitpart` | `pointfloat`) `exponent`
1034-
digitpart: `digit` (["_"] `digit`)*
1035-
fraction: "." `digitpart`
1036-
exponent: ("e" | "E") ["+" | "-"] `digitpart`
1035+
They consist of *integer* and *fraction* parts, each composed of decimal digits.
1036+
The parts are separated by a decimal point, ``.``::
1037+
1038+
2.71828
1039+
4.0
1040+
1041+
Unlike in integer literals, leading zeros are allowed in the numeric parts.
1042+
For example, ``077.010`` is legal, and denotes the same number as ``77.10``.
1043+
1044+
As in integer literals, single underscores may occur between digits to help
1045+
readability::
1046+
1047+
96_485.332_123
1048+
3.14_15_93
1049+
1050+
Either of these parts, but not both, can be empty. For example::
10371051

1038-
Note that the integer and exponent parts are always interpreted using radix 10.
1039-
For example, ``077e010`` is legal, and denotes the same number as ``77e10``. The
1040-
allowed range of floating-point literals is implementation-dependent. As in
1041-
integer literals, underscores are supported for digit grouping.
1052+
10. # (equivalent to 10.0)
1053+
.001 # (equivalent to 0.001)
10421054

1043-
Some examples of floating-point literals::
1055+
Optionally, the integer and fraction may be followed by an *exponent*:
1056+
the letter ``e`` or ``E``, followed by an optional sign, ``+`` or ``-``,
1057+
and a number in the same format as the integer and fraction parts.
1058+
The ``e`` or ``E`` represents "times ten raised to the power of"::
10441059

1045-
3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
1060+
1.0e3 # (represents 1.0×10³, or 1000.0)
1061+
1.166e-5 # (represents 1.166×10⁻⁵, or 0.00001166)
1062+
6.02214076e+23 # (represents 6.02214076×10²³, or 602214076000000000000000.)
1063+
1064+
In floats with only integer and exponent parts, the decimal point may be
1065+
omitted::
1066+
1067+
1e3 # (equivalent to 1.e3 and 1.0e3)
1068+
0e0 # (equivalent to 0.)
1069+
1070+
Formally, floating-point literals are described by the following
1071+
lexical definitions:
1072+
1073+
.. grammar-snippet::
1074+
:group: python-grammar
1075+
1076+
floatnumber:
1077+
| `digitpart` "." [`digitpart`] [`exponent`]
1078+
| "." `digitpart` [`exponent`]
1079+
| `digitpart` `exponent`
1080+
digitpart: `digit` (["_"] `digit`)*
1081+
exponent: ("e" | "E") ["+" | "-"] `digitpart`
10461082

10471083
.. versionchanged:: 3.6
10481084
Underscores are now allowed for grouping purposes in literals.
@@ -1053,7 +1089,7 @@ Some examples of floating-point literals::
10531089
.. _imaginary:
10541090

10551091
Imaginary literals
1056-
------------------
1092+
^^^^^^^^^^^^^^^^^^
10571093

10581094
Imaginary literals are described by the following lexical definitions:
10591095

0 commit comments

Comments
 (0)