The TikZ and PGF Packages
Manual for version 3.1.10
Mathematical and ObjectOriented Engines
95 Additional Mathematical Commands¶
Instead of parsing and evaluating complex expressions, you can also use the mathematical engine to evaluate a single mathematical operation. The macros used for many of these computations are listed above in Section 94.3. pgf also provides some additional commands which are shown below:
95.1 Basic arithmetic functions¶
In addition to the commands described in Section 94.3.1, the following command is provided:

\pgfmathreciprocal{⟨x⟩} ¶
Defines \pgfmathresult as \(1\div \meta {x}\). This provides greatest accuracy when x is small.
95.2 Comparison and logical functions¶
In addition to the commands described in Section 94.3.5, the following command was provided by Christian Feuersänger:

\pgfmathapproxequalto{⟨x⟩}{⟨y⟩} ¶
Defines \pgfmathresult 1.0 if \(\rvert \meta {x}  \meta {y} \lvert < 0.0001\), but 0.0 otherwise. As a sideeffect, the global boolean \ifpgfmathcomparison will be set accordingly.
95.3 PseudoRandom Numbers¶
In addition to the commands described in Section 94.3.6, the following commands are provided:

\pgfmathgeneratepseudorandomnumber ¶
Defines \pgfmathresult as a pseudorandom integer between 1 and \(2^{31}1\). This uses a linear congruency generator, based on ideas of Erich Janka.

\pgfmathrandominteger{⟨macro⟩}{⟨minimum⟩}{⟨maximum⟩} ¶
This defines ⟨macro⟩ as a pseudorandomly generated integer from the range ⟨minimum⟩ to ⟨maximum⟩ (inclusive).

\pgfmathdeclarerandomlist{⟨list name⟩}{{⟨item1⟩}{⟨item2⟩}...} ¶
This creates a list of items with the name ⟨list name⟩.

\pgfmathrandomitem{⟨macro⟩}{⟨list name⟩} ¶
Select an item from a random list ⟨list name⟩. The selected item is placed in ⟨macro⟩.

\pgfmathsetseed{⟨integer⟩} ¶
Explicitly sets the seed for the pseudorandom number generator. By default it is set to the value of \time\(\times \)\year.
95.4 Base Conversion¶
pgf provides limited support for conversion between representations of numbers. Currently the numbers must be positive integers in the range \(0\) to \(2^{31}1\), and the bases in the range \(2\) to \(36\). All digits representing numbers greater than 9 (in base ten), are alphabetic, but may be upper or lower case.
In addition to the commands described in Section 94.3.7, the following commands are provided:

\pgfmathbasetodec{⟨macro⟩}{⟨number⟩}{⟨base⟩} ¶
Defines ⟨macro⟩ as the result of converting ⟨number⟩ from base ⟨base⟩ to base 10. Alphabetic digits can be upper or lower case.
Note that, as usual in TeX, the braces around an argument can be omitted if the argument is just a single token (a macro name is a single token).

\pgfmathdectobase{⟨macro⟩}{⟨number⟩}{⟨base⟩} ¶
Defines ⟨macro⟩ as the result of converting ⟨number⟩ from base 10 to base ⟨base⟩. Any resulting alphabetic digits are in lower case.

\pgfmathdectoBase{⟨macro⟩}{⟨number⟩}{⟨base⟩} ¶
Defines ⟨macro⟩ as the result of converting ⟨number⟩ from base 10 to base ⟨base⟩. Any resulting alphabetic digits are in upper case.

\pgfmathbasetobase{⟨macro⟩}{⟨number⟩}{⟨base1⟩}{⟨base2⟩} ¶
Defines ⟨macro⟩ as the result of converting ⟨number⟩ from base ⟨base1⟩ to base ⟨base2⟩. Alphabetic digits in ⟨number⟩ can be upper or lower case, but any resulting alphabetic digits are in lower case.

\pgfmathbasetoBase{⟨macro⟩}{⟨number⟩}{⟨base1⟩}{⟨base2⟩} ¶
Defines ⟨macro⟩ as the result of converting ⟨number⟩ from base ⟨base1⟩ to base ⟨base2⟩. Alphabetic digits in ⟨number⟩ can be upper or lower case, but any resulting alphabetic digits are in upper case.

\pgfmathsetbasenumberlength{⟨integer⟩} ¶
Sets the number of digits in the result of a base conversion to ⟨integer⟩. If the result of a conversion has less digits than this number, it is prefixed with zeros.

\pgfmathtodigitlist{⟨macro⟩}{⟨number⟩} ¶
This command converts ⟨number⟩ into a commaseparated list of digits and stores the result in ⟨macro⟩. The {⟨number⟩} is not parsed before processing.
95.5 Angle Computations¶
Unlike the rest of the math engine, which is a “standalone” package, the following commands only work in conjunction with the core of pgf.

\pgfmathanglebetweenpoints{⟨p⟩}{⟨q⟩} ¶
Returns the angle of a line from ⟨p⟩ to ⟨q⟩ relative to a line going straight right from ⟨p⟩.

\pgfmathanglebetweenlines{⟨\(p_1\)⟩}{⟨\(q_1\)⟩}{⟨\(p_2\)⟩}{⟨\(q_2\)⟩} ¶
Returns the clockwise angle between a line going through \(p_1\) and \(q_1\) and a line going through \(p_2\) and \(q_2\).