From 76dd85edfacb1909a7de5807d369e69c8558deac Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Wed, 23 Nov 2016 15:18:46 +0100 Subject: [PATCH 1/3] docs: a bit more about literals --- docs/types.rst | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index 21575cfb2..85309f603 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -169,9 +169,13 @@ Fixed Point Numbers Rational and Integer Literals ----------------------------- -All number literals retain arbitrary precision until they are converted to a non-literal type (i.e. by -using them together with a non-literal type). This means that computations do not overflow but also -divisions do not truncate. +Integer literals and rational number literals belong to a special type. +The number literal type contains not just single literals +but all number literal expressions (i.e. the expressions that contain only number literals and operators). +Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by +using them together with a non-literal expression). +This means that computations do not overflow and divisions do not truncate +in number literal expressions. For example, ``(2**800 + 1) - 2**800`` results in the constant ``1`` (of type ``uint8``) although intermediate results would not even fit the machine word size. Furthermore, ``.5 * 8`` results @@ -185,7 +189,7 @@ In ``var x = 1/4;``, ``x`` will receive the type ``ufixed0x8`` while in ``var x the type ``ufixed0x256`` because ``1/3`` is not finitely representable in binary and will thus be approximated. -Any operator that can be applied to integers can also be applied to literal expressions as +Any operator that can be applied to integers can also be applied to number literal expressions as long as the operands are integers. If any of the two is fractional, bit operations are disallowed and exponentiation is disallowed if the exponent is fractional (because that might result in a non-rational number). @@ -200,7 +204,7 @@ a non-rational number). Division on integer literals used to truncate in earlier versions, but it will now convert into a rational number, i.e. ``5 / 2`` is not equal to ``2``, but to ``2.5``. .. note:: - Literal expressions are converted to a permanent type as soon as they are used with other + Number literal expressions are converted into a non-literal type as soon as they are used with non-literal expressions. Even though we know that the value of the expression assigned to ``b`` in the following example evaluates to an integer, it still uses fixed point types (and not rational number literals) in between and so the code @@ -216,7 +220,7 @@ a non-rational number). String Literals --------------- -String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``. +String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). They do not imply trailing zeroes as in C; `"foo"`` represents three bytes not four. As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``. String literals support escape characters, such as ``\n``, ``\xNN`` and ``\uNNNN``. ``\xNN`` takes a hex value and inserts the appropriate byte, while ``\uNNNN`` takes a Unicode codepoint and inserts an UTF-8 sequence. From a747f1d2c32c691b00f3b24fead8436195eb555e Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Fri, 25 Nov 2016 10:54:40 +0100 Subject: [PATCH 2/3] docs: clarify how many number literal types are there --- docs/types.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index 85309f603..83b0a0980 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -169,9 +169,13 @@ Fixed Point Numbers Rational and Integer Literals ----------------------------- -Integer literals and rational number literals belong to a special type. -The number literal type contains not just single literals -but all number literal expressions (i.e. the expressions that contain only number literals and operators). +Solidity has a number literal type for each rational number. +Integer literals and rational number literals belong to number literal types. +Moreover, all number literal expressions (i.e. the expressions that +contain only number literals and operators) belong to number literal +types. So the number literal expressions `1 + 2` and `2 + 1` both +belong to the same number literal type for the rational number three. + Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by using them together with a non-literal expression). This means that computations do not overflow and divisions do not truncate From a755805879eb3cfb22bbe3845dc61a288f64a0e0 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Fri, 25 Nov 2016 14:51:38 +0100 Subject: [PATCH 3/3] Update types.rst --- docs/types.rst | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index 83b0a0980..0436fc708 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -169,13 +169,6 @@ Fixed Point Numbers Rational and Integer Literals ----------------------------- -Solidity has a number literal type for each rational number. -Integer literals and rational number literals belong to number literal types. -Moreover, all number literal expressions (i.e. the expressions that -contain only number literals and operators) belong to number literal -types. So the number literal expressions `1 + 2` and `2 + 1` both -belong to the same number literal type for the rational number three. - Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by using them together with a non-literal expression). This means that computations do not overflow and divisions do not truncate @@ -198,6 +191,14 @@ long as the operands are integers. If any of the two is fractional, bit operatio and exponentiation is disallowed if the exponent is fractional (because that might result in a non-rational number). +.. note:: + Solidity has a number literal type for each rational number. + Integer literals and rational number literals belong to number literal types. + Moreover, all number literal expressions (i.e. the expressions that + contain only number literals and operators) belong to number literal + types. So the number literal expressions `1 + 2` and `2 + 1` both + belong to the same number literal type for the rational number three. + .. note:: Most finite decimal fractions like ``5.3743`` are not finitely representable in binary. The correct type for ``5.3743`` is ``ufixed8x248`` because that allows to best approximate the number. If you want to