Skip to content

Commit 60a21c6

Browse files
committed
Merge pull request #427 from chriseth/intlit
Warning about using integer literals in division.
2 parents 172beae + 62d8be9 commit 60a21c6

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/types.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Operators:
5151
* Bit operators: `&`, `|`, `^` (bitwise exclusive or), `~` (bitwise negation)
5252
* Arithmetic operators: `+`, `-`, unary `-`, unary `+`, `*`, `/`, `%` (remainder), `**` (exponentiation)
5353

54+
Division always truncates (it just maps to the DIV opcode of the EVM), but it does not truncate if both
55+
operators are :ref:`literals<integer_literals>` (or literal expressions).
56+
5457
.. index:: address, balance, send, call, callcode
5558

5659
Address
@@ -132,13 +135,19 @@ number of bytes, always use one of `bytes1` to `bytes32` because they are much c
132135

133136
.. index:: literal, literal;integer
134137

138+
.. _integer_literals:
139+
135140
Integer Literals
136141
-----------------
137142

138143
Integer Literals are arbitrary precision integers until they are used together with a non-literal. In `var x = 1 - 2;`, for example, the value of `1 - 2` is `-1`, which is assigned to `x` and thus `x` receives the type `int8` -- the smallest type that contains `-1`, although the natural types of `1` and `2` are actually `uint8`.
139144

140145
It is even possible to temporarily exceed the maximum of 256 bits as long as only integer literals are used for the computation: `var x = (0xffffffffffffffffffff * 0xffffffffffffffffffff) * 0;` Here, `x` will have the value `0` and thus the type `uint8`.
141146

147+
.. warning::
148+
Divison on integer literals used to truncate in earlier versions, but it will actually convert into a rational number in the future, i.e. `1/2` is not equal to `0`, but to `0.5`.
149+
150+
142151
.. index:: literal, literal;string, string
143152

144153
String Literals

0 commit comments

Comments
 (0)