summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-math-parsing.tex
blob: 7ec64df937cb0ffd214a8e8032eaabcce8a96e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
% Copyright 2007 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
%

\section{Evaluating Mathematical Expressions}

The easiest way of using \pgfname's mathematical engine is to provide
a mathematical expression given in the usual infix notation (such as
|1cm+4*2cm/5.5| or |2*3+3*sin(30)|). This expression can be parsed by
the mathematical engine and the result be placed in a dimension
register, a counter, or a macro. Supported are infix mathematical
operations involving integers and non-integers, with or without
units.

It should be noted that all 
calculations must not exceed $\pm16383.99999$ at \emph{any} point, 
because the underlying algorithms rely on \TeX{} dimensions. This
means that many of the underlying algorithms are necessarily
approximate. It also means that some of the algorithms are not very
fast. \TeX{} is, after all, a typesetting language and not ideally
suited to relatively advanced mathematical operations. However, it is
possible to change the algorithms as described in
Section~\ref{pgfmath-reimplement}. 

In the present section, the high-level macros for parsing an
expression are explained first, then the syntax for expression is
explained.


\subsection{Commands for Parsing Expressions}

\label{pgfmath-registers}

\label{pgfmath-parsing}

The basic command for invoking the parser of \pgfname's mathematical
engine is the following:

\begin{command}{\pgfmathparse\marg{expression}}
  This macro parses \meta{expression} and returns the result without
  units in  the macro |\pgfmathresult|.

  \example |\pgfmathparse{2pt+3.5pt}| will set |\pgfmathresult| to the
  text |5.5|.

  In the following, the special properties of this command are
  explained. The exact syntax of mathematical expressions is explained
  in Section~\ref{pgfmath-syntax}.

  \begin{itemize}
  \item
    The result stored in the macro |\pgfmathresult| is a decimal
    \emph{without units}. This is true regardless of whether the
    \meta{expression} contains any unit specification. But, any units
    specified will be converted to points first.
\begin{codeexample}[]
\pgfmathparse{2pt+3.4pt} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{2cm+3.4cm} \pgfmathresult
\end{codeexample}

  \item If no units are specified \emph{at any point} in the 
    expression, the result will be multiplied by the value in 
    |\pgfmathresultunitscale|, which can be a number or a dimension 
    (which will be converted to points). By default it is set to 1, 
    but can be changed with |\pgfmathsetresultunitscale|. Note that 
    the result will still be a number \emph{without} units.

\begin{codeexample}[]
\pgfmathparse{2pt+3.4pt} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathsetresultunitscale{1cm}
\pgfmathparse{2+3.4} \pgfmathresult
\end{codeexample}

    \pgfmathsetresultunitscale{1pt}
    
  \item You can check whether an expression contained a unit using 
    the \TeX-if |\||ifpgfmathunitsdeclared|. After a call
    of |\pgfmathparse| this if will be true exactly if some unit was
    encountered in the expression.
    
  \item The parser handles numbers with or without units regardless
    of the operation.

\begin{codeexample}[]
\pgfmathparse{54pt/3cm*2.1} \pgfmathresult
\end{codeexample}

  \item the parser can cope with \TeX{} registers, including those 
    preceded by |\the|.

    \makeatletter

\begin{codeexample}[]
\pgf@x=12.34pt
\c@pgf@counta=5
\pgfmathparse{\pgf@x+\c@pgf@counta*6} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgf@x=56.78pt
\pgfmathparse{\pgf@x+\the\pgf@x} \pgfmathresult
\end{codeexample}

  \item \TeX{} dimension registers can be multiplied without the |*| 
    operator by preceding them with a number (\emph{not} a function),
     or a count register.
	 
\begin{codeexample}[]
\c@pgf@counta=-4
\pgf@x=10pt
\pgfmathparse{.5\pgf@x-\c@pgf@counta\pgf@x} \pgfmathresult
\end{codeexample}

  \item Parenthesis can be used to group operations.

\begin{codeexample}[]
\pgfmathparse{(4pt+0.5)*3} \pgfmathresult
\end{codeexample}

  \item functions are recognized, so it is possible to parse
    |sin(.5*pi r)*60|, which means ``the sine of $0.5$ times $\pi$ 
    radians, multiplied by 60''. The argument of most functions can
    be any expression.

\begin{codeexample}[]
\pgfmathparse{sin(pi/2 r)*60} \pgfmathresult
\end{codeexample}

  \item Scientific notation in the form |1.234e+4| is recognised (but
  the restriction on the range of values still applies). The exponent
  symbol can be upper or lower case (i.e., |E| or |e|). 
  
\begin{codeexample}[]
\pgfmathparse{1.234567891e-2} \pgfmathresult
\end{codeexample}
\begin{codeexample}[]
\pgfmathparse{1.234567891e4} \pgfmathresult
\end{codeexample}
  \end{itemize}
\end{command}

\begin{command}{\pgfmathqparse\marg{expression}}
  This macro is similar to |\pgfmathparse|: it parses 
  \meta{expression} and returns the result in the macro 
  |\pgfmathresult|. It differs in two respects. Firstly, 
  |\pgfmathqparse| does not parse functions or scientific
  notation. 
  Secondly, numbers in \meta{expression} \emph{must}
  specify a \TeX{} unit (except in such instances as |0.5\pgf@x|), 
  which greatly simplifies the problem of parsing 
  of non-integers. As a result of these restrictions |\pgfmathqparse| 
  is about twice as fast as |\pgfmathparse|. Note that the result 
  will still be a number \emph{without} units.	
\end{command}

\begin{command}{\pgfmathsetresultunitscale\marg{number or dimension}}
  Sets the value in |\pgfmathresultunitscale|, which scales the result
  of an expression parsed with |\pgfmathparse|, if that expression
  contains no units \emph{at any point}. The argument can be an integer,
  non-integer or a dimension, but the result will still be a number 
  \emph{without} units. Note, that this will affect |\pgfmathsetlength| 
  and friends, but not if the expression starts with |+| (which
  switches parsing off). By default the value in
  |\pgfmathresultunitscale| is 1. 
\end{command}

Instead of the |\pgfmathparse| macro you can also wrapper commands,
whose usage is very similar to their cousins in the \calcname{} 
package. The only difference is that the expressions can be any
expression that is handled by |\pgfmathparse|.

For all of the following commands, if \meta{expression} starts with
|+|, no parsing is done and a simple assignment or increment is done
using normal \TeX\ assignments or increments. This will be orders of
magnitude faster than calling the parser. 

\begin{command}{\pgfmathsetlength\marg{dimension register}\marg{expression}}
  Sets the length of the \TeX{} \meta{dimension register}, to the value
  (in points) specified by \meta{expression}. The \meta{expression}
  will be parsed using |\pgfmathparse|.
\end{command}

\begin{command}{\pgfmathaddtolength\marg{dimension register}\marg{expression}}
  Adds the value (in points) of \meta{expression} to the \TeX{} 
  \meta{dimension register}. 
\end{command}

\begin{command}{\pgfmathsetcount\marg{count register}\marg{expression}}
  Sets the value of the \TeX{} \meta{count register}, to the 
  \emph{truncated} value specified by \meta{expression}. 
\end{command}

\begin{command}{\pgfmathaddtocount\marg{count register}\marg{expression}}
  Adds the \emph{truncated} value  of \meta{expression} to the \TeX{} 
  \meta{count register}.
\end{command}

\begin{command}{\pgfmathsetcounter\marg{counter}\marg{expression}}
  Sets the value of the \LaTeX{} \meta{counter}, to the \emph{truncated} 
  value specified by \meta{expression}. 
\end{command}

\begin{command}{\pgfmathaddtocounter\marg{counter}\marg{expression}}
  Adds the \emph{truncated} value  of \meta{expression} to 
  \meta{counter}.
\end{command}

% \begin{command}{\pgfmathnewcounter\marg{counter}}
%   This is simply a version of the \LaTeX{} macro |\newcounter|, 
%   implemented to maintain consistency (consistency is good,
%   inconsistency is evil). Considering |\pgfmathnewcounter{foo}|, this
%   creates a new count register |\c@foo|, and a macro |\thefoo|, which
%   returns the value in |\c@foo|.
% \end{command}

\begin{command}{\pgfmathsetmacro\marg{macro}\marg{expression}}
  Defines \meta{macro} as the  value of \meta{expression}. The result
  is a decimal \emph{without} units.
\end{command}

\begin{command}{\pgfmathsetlengthmacro\marg{macro}\marg{expression}}
  Defines \meta{macro} as the value of \meta{expression} 
  \LaTeX{}\emph{in points}.
\end{command}

\begin{command}{\pgfmathtruncatemacro\marg{macro}\marg{expression}}
  Defines \meta{macro} as the truncated value of \meta{expression}.
\end{command}



\subsection{Syntax for mathematical expressions}

\label{pgfmath-syntax}

The syntax for the expressions recognized by |\pgfmathparse| and
friends is straightfoward, and the following operations and 
functions are currently recognized:

\begin{math-operator}{\mvar{x}\ +\ \mvar{y}}
	Adds \mvar{y} to \mvar{x}.
	
\begin{codeexample}[]
\pgfmathparse{4+2pt} \pgfmathresult
\end{codeexample}
\end{math-operator}

\begin{math-operator}{\mvar{x}\ -\ \mvar{y}}
	Subtracts \mvar{y} from  \mvar{x}.
	
\begin{codeexample}[]
\pgfmathparse{155.35-4cm} \pgfmathresult
\end{codeexample}
\end{math-operator}
\begin{math-operator}{\mvar{x}\ *\ \mvar{y}}
	Multiplies \mvar{x} by  \mvar{y}.
	
\begin{codeexample}[]
\pgfmathparse{3.9pt*4.56} \pgfmathresult
\end{codeexample}

\end{math-operator}
\begin{math-operator}{\mvar{x}\ /\ \mvar{y}}
	Divides \mvar{x} by  \mvar{y}.
	
\begin{codeexample}[]
\pgfmathparse{-31.6pt/17} \pgfmathresult
\end{codeexample}

\end{math-operator}
\begin{math-operator}{\mvar{x}\ {\char94}\ \mvar{y}} 

Raises \mvar{x} to the power \mvar{y}. For greatest accuracy \mvar{y}
should be an integer. If \mvar{y} is not an integer the actual
calculation will be an approximation of $e^{y\ln(x)}$.

{
\catcode`\^=7

\begin{codeexample}[]
\pgfmathparse{2.3^4} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{2^-4} \pgfmathresult
\end{codeexample}
}
\end{math-operator}

\begin{math-operator}{\mvar{x}\ ==\ \mvar{y}} 

	This evaluates to |1| if \mvar{x} equals \mvar{y}, or |0| if \mvar{x}
	does not equal \mvar{y}. 
	Note that equalities (and inequalities) are evaluated left to right, 
	and are only evaluated when another equality (or inequality) operator 
	is scanned, or the end of the current group or parse is reached. So 
	|5+4==3+2==9| results in |0| because |5+4| does not equal |3+2|, 
	resulting in zero, and the second equality is therefore evaluating 
	|0==9|.

\begin{codeexample}[]
\pgfmathparse{3*5==15} \pgfmathresult
\end{codeexample}

\end{math-operator}


\begin{math-operator}{\mvar{x}\ >\ \mvar{y}} 

	This evaluates to |1| if \mvar{x} is greater than \mvar{y}, or |0| if 
	\mvar{x} is smaller or equal to \mvar{y}.
	
\begin{codeexample}[]
\pgfmathparse{17>4.2*1.97+4} \pgfmathresult
\end{codeexample}

\end{math-operator}

\begin{math-operator}{\mvar{x}\ <\ \mvar{y}}

	This evaluates to |1| if \mvar{x} is smaller than \mvar{y}, or |0| if
	\mvar{x} is greater or equal to \mvar{y}.
	
\begin{codeexample}[]
\pgfmathparse{2<-5.2/-3.6-2} \pgfmathresult
\end{codeexample}

\end{math-operator}

\begin{math-function}{mod(\mvar{x},\mvar{y})}
	This evaluates \mvar{x} modulo \mvar{y} (using truncated division).
	This function cannot be nested inside itself or the functions |max|, 
	|min| or |pow|.

\begin{codeexample}[]
\pgfmathparse{mod(20,6)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{max(\mvar{x},\mvar{y})}
	This evaluates to the maximum of \mvar{x} or \mvar{y}. This function 
	cannot be nested inside itself or the functions |min|, |mod| or 
	|pow|.

\begin{codeexample}[]
\pgfmathparse{max(17,23)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{min(\mvar{x},\mvar{y})}
	This evaluates to the minimum of \mvar{x} or \mvar{y}. This function 
	cannot be nested inside itself or the functions |max|, |mod| or 
	|pow|.

\begin{codeexample}[]
\pgfmathparse{min(17,23)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{abs(\mvar{x})} 

	Evaluates the absolute value of $x$.
	
\begin{codeexample}[]
\pgfmathparse{abs(-5)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{-abs(4*-3)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{round(\mvar{x})}

	Rounds \mvar{x} to the nearest integer. It uses ``asymmetric half-up'' 
	rounding. So |1.5| is rounded to |2|, but |-1.5| is rounded to |-2| 
	(\emph{not} |0|).

\begin{codeexample}[]
\pgfmathparse{round(32.5/17)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{round(398/12)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{floor(\mvar{x})}

	Rounds \mvar{x} down to the nearest integer. 
	
\begin{codeexample}[]
\pgfmathparse{floor(32.5/17)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{floor(398/12)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{ceil(\mvar{x})}

	Rounds \mvar{x} up to the nearest integer. 

\begin{codeexample}[]
\pgfmathparse{ceil(32.5/17)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{ceil(398/12)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{exp(\mvar{x})}
{
\catcode`\^=7

	Maclaurin series for $e^x$. 
}	
\begin{codeexample}[]
\pgfmathparse{exp(1)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{exp(2.34)} \pgfmathresult
\end{codeexample}

\end{math-function}


\begin{math-function}{ln(\mvar{x})}
{
\catcode`\^=7

	An approximation for for $\ln(\textrm{\mvar{x}})$. 
}	
\begin{codeexample}[]
\pgfmathparse{ln(10)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{ln(exp(5))} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{pow(\mvar{x},\mvar{y})}

 Raises \mvar{x} to the power \mvar{y}. 

\begin{codeexample}[]
\pgfmathparse{pow(2,7)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{sqrt(\mvar{x})}

 Calculates $\sqrt{\textrm{\mvar{x}}}$.

\begin{codeexample}[]
\pgfmathparse{sqrt(10)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{sqrt(8765.432)}  \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{veclen(\mvar{x},\mvar{y})}

 Calculates $\sqrt{\left(\textrm{\mvar{x}}^2+\textrm{\mvar{y}}^2\right)}$.

\begin{codeexample}[]
\pgfmathparse{veclen(12,5)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-constant}{pi}

	The constant $\pi=3.14159$.
	
\begin{codeexample}[]
\pgfmathparse{pi} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{pi r} \pgfmathresult
\end{codeexample}

\end{math-constant}

\begin{math-operator}{\mvar{x}\ r}

	This converts \mvar{x} from radians to degrees. Note that |r| will 
	evaluate any preceding series of multiplication or division 
	\emph{before} conversion, but not other operations. So |3*4/6r| 
	converts 2 radians to degrees, but |3-4+6r|, converts 6 radians to
	degrees and adds the result to |-1|.

\begin{codeexample}[]
\pgfmathparse{2*pi r-pi r} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{2*pi/8 r} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{sin(3*pi/2r)*60} \pgfmathresult
\end{codeexample}

\end{math-operator}

\begin{math-function}{rad(\mvar{x})}

	Convert \mvar{x} to radians. \mvar{x} is assumed to be in degrees.
	
\begin{codeexample}[]
\pgfmathparse{rad(90)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{deg(\mvar{x})}

	Convert \mvar{x} to degrees. \mvar{x} is assumed to be in radians.
	
\begin{codeexample}[]
\pgfmathparse{deg(3*pi/2)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{sin(\mvar{x})}

	Sine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[]
\pgfmathparse{sin(60)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{sin(pi/3 r)}
\end{codeexample}

\end{math-function}

\begin{math-function}{cos(\mvar{x})}

	Cosine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.

\begin{codeexample}[]
\pgfmathparse{cos(60)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{cos(pi/3 r)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{tan(\mvar{x})}

	Tangent of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[]
\pgfmathparse{tan(45)} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{tan(2*pi/8 r)} \pgfmathresult
\end{codeexample}

\end{math-function}


\begin{math-function}{sec(\mvar{x})}

	Secant of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.

\begin{codeexample}[]
\pgfmathparse{sec(45)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{cosec(\mvar{x})}

	Cosecant of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[]
\pgfmathparse{cosec(30)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{cot(\mvar{x})}

	Cotangent of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[]
\pgfmathparse{cot(15)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{asin(\mvar{x})}

	Arcsine of \mvar{x}. The result is in degrees and in the range $\pm90^\circ$.

\begin{codeexample}[]
\pgfmathparse{asin(0.7071)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{acos(\mvar{x})}

	Arccosine of \mvar{x} in degrees. The result is in the range $\pm90^\circ$.


\begin{codeexample}[]
\pgfmathparse{acos(0.5)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{atan(\mvar{x})}

	Arctangent of $x$ in degrees. 

\begin{codeexample}[]
\pgfmathparse{atan(1)} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{rnd}

	Generates a pseudo-random number between 0 and 1.

\begin{codeexample}[]
\pgfmathparse{rnd} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{2*rnd} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{-rnd+5} \pgfmathresult
\end{codeexample}

\end{math-function}

\begin{math-function}{rand}

	Generates a pseudo-random number between -1 and 1.

\begin{codeexample}[]
\pgfmathparse{rand} \pgfmathresult
\end{codeexample}

\begin{codeexample}[]
\pgfmathparse{rand*15} \pgfmathresult
\end{codeexample} 

\end{math-function}