about summary refs log tree commit diff
path: root/Doc/Zsh/mod_mathfunc.yo
blob: 8b72de3ab22365d703e11df4cd4e8b6e049ba972 (plain) (blame)
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
COMMENT(!MOD!zsh/mathfunc
Standard scientific functions for use in mathematical evaluations.
!MOD!)
cindex(functions, mathematical)
cindex(mathematical functions)
The tt(zsh/mathfunc) module provides standard
mathematical functions for use when
evaluating mathematical formulae.  The syntax agrees with normal C and
FORTRAN conventions, for example,

example((( f = sin+LPAR()0.3+RPAR() )))

assigns the sine of 0.3 to the parameter f.

Most functions take floating point arguments and return a floating point
value.  However, any necessary conversions from or to integer type will be
performed automatically by the shell.  Apart from tt(atan) with a second
argument and the tt(abs), tt(int) and tt(float) functions, all functions
behave as noted in the manual page for the corresponding C function,
except that any arguments out of range for the function in question will be
detected by the shell and an error reported.

The following functions take a single floating point argument: tt(acos),
tt(acosh), tt(asin), tt(asinh), tt(atan), tt(atanh), tt(cbrt), tt(ceil),
tt(cos), tt(cosh), tt(erf), tt(erfc), tt(exp), tt(expm1), tt(fabs),
tt(floor), tt(gamma), tt(j0), tt(j1), tt(lgamma), tt(log), tt(log10),
tt(log1p), tt(logb), tt(sin), tt(sinh), tt(sqrt), tt(tan), tt(tanh),
tt(y0), tt(y1).  The tt(atan) function can optionally take a second
argument, in which case it behaves like the C function tt(atan2).
The tt(ilogb) function takes a single floating point argument, but
returns an integer.

The function tt(signgam) takes no arguments, and returns an integer, which
is the C variable of the same name, as described in manref(gamma)(3).  Note
that it is therefore only useful immediately after a call to tt(gamma) or
tt(lgamma).  Note also that `tt(signgam+LPAR()RPAR())' and `tt(signgam)' are
distinct expressions.

The functions tt(min), tt(max), and tt(sum) are defined not in this module
but in the tt(zmathfunc) autoloadable function, described in
ifzman(the section `Mathematical Functions' in zmanref(zshcontrib))\
ifnzman(noderef(Mathematical Functions)).

The following functions take two floating point arguments: tt(copysign),
tt(fmod), tt(hypot), tt(nextafter).

The following take an integer first argument and a floating point second
argument: tt(jn), tt(yn).

The following take a floating point first argument and an integer second
argument: tt(ldexp), tt(scalb).

The function tt(abs) does not convert the type of its single argument; it
returns the absolute value of either a floating point number or an
integer.  The functions tt(float) and tt(int) convert their arguments into
a floating point or integer value (by truncation) respectively.

Note that the C tt(pow) function is available in ordinary math evaluation
as the `tt(**)' operator and is not provided here.

The function tt(rand48) is available if your system's mathematical library
has the function tt(erand48(3)).  It returns a pseudo-random floating point
number between 0 and 1.  It takes a single string optional argument.

If the argument is not present, the random number seed is initialised by
three calls to the tt(rand+LPAR()3+RPAR()) function --- this produces the
same random
numbers as the next three values of tt($RANDOM).

If the argument is present, it gives the name of a scalar parameter where
the current random number seed will be stored.  On the first call, the
value must contain at least twelve hexadecimal digits (the remainder of the
string is ignored), or the seed will be initialised in the same manner as
for a call to tt(rand48) with no argument.  Subsequent calls to
tt(rand48)LPAR()var(param)RPAR() will then maintain the seed in the
parameter var(param) as a string of twelve hexadecimal digits, with no base
signifier.  The random number sequences for different parameters are
completely independent, and are also independent from that used by calls to
tt(rand48) with no argument.

For example, consider

example(print $(( rand48(seed) ))
print $(( rand48() ))
print $(( rand48(seed) )))

Assuming tt($seed) does not exist, it will be initialised by the first
call.  In the second call, the default seed is initialised; note, however,
that because of the properties of tt(rand+LPAR()RPAR()) there is a
correlation between
the seeds used for the two initialisations, so for more secure uses, you
should generate your own 12-byte seed.  The third call returns to the same
sequence of random numbers used in the first call, unaffected by the
intervening tt(rand48()).