| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-2000 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2005 Kevin B. Kenny <[email protected]>. All rights reserved |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH mathfunc n 8.5 Tcl "Tcl Mathematical Functions" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
mathfunc \- Mathematical functions for Tcl expressions |
| 15 |
.SH SYNOPSIS |
| 16 |
package require \fBTcl 8.5\fR |
| 17 |
.sp |
| 18 |
\fB::tcl::mathfunc::abs\fR \fIarg\fR |
| 19 |
.br |
| 20 |
\fB::tcl::mathfunc::acos\fR \fIarg\fR |
| 21 |
.br |
| 22 |
\fB::tcl::mathfunc::asin\fR \fIarg\fR |
| 23 |
.br |
| 24 |
\fB::tcl::mathfunc::atan\fR \fIarg\fR |
| 25 |
.br |
| 26 |
\fB::tcl::mathfunc::atan2\fR \fIy\fR \fIx\fR |
| 27 |
.br |
| 28 |
\fB::tcl::mathfunc::bool\fR \fIarg\fR |
| 29 |
.br |
| 30 |
\fB::tcl::mathfunc::ceil\fR \fIarg\fR |
| 31 |
.br |
| 32 |
\fB::tcl::mathfunc::cos\fR \fIarg\fR |
| 33 |
.br |
| 34 |
\fB::tcl::mathfunc::cosh\fR \fIarg\fR |
| 35 |
.br |
| 36 |
\fB::tcl::mathfunc::double\fR \fIarg\fR |
| 37 |
.br |
| 38 |
\fB::tcl::mathfunc::entier\fR \fIarg\fR |
| 39 |
.br |
| 40 |
\fB::tcl::mathfunc::exp\fR \fIarg\fR |
| 41 |
.br |
| 42 |
\fB::tcl::mathfunc::floor\fR \fIarg\fR |
| 43 |
.br |
| 44 |
\fB::tcl::mathfunc::fmod\fR \fIx\fR \fIy\fR |
| 45 |
.br |
| 46 |
\fB::tcl::mathfunc::hypot\fR \fIx\fR \fIy\fR |
| 47 |
.br |
| 48 |
\fB::tcl::mathfunc::int\fR \fIarg\fR |
| 49 |
.br |
| 50 |
\fB::tcl::mathfunc::isqrt\fR \fIarg\fR |
| 51 |
.br |
| 52 |
\fB::tcl::mathfunc::log\fR \fIarg\fR |
| 53 |
.br |
| 54 |
\fB::tcl::mathfunc::log10\fR \fIarg\fR |
| 55 |
.br |
| 56 |
\fB::tcl::mathfunc::max\fR \fIarg\fR ?\fIarg\fR ...? |
| 57 |
.br |
| 58 |
\fB::tcl::mathfunc::min\fR \fIarg\fR ?\fIarg\fR ...? |
| 59 |
.br |
| 60 |
\fB::tcl::mathfunc::pow\fR \fIx\fR \fIy\fR |
| 61 |
.br |
| 62 |
\fB::tcl::mathfunc::rand\fR |
| 63 |
.br |
| 64 |
\fB::tcl::mathfunc::round\fR \fIarg\fR |
| 65 |
.br |
| 66 |
\fB::tcl::mathfunc::sin\fR \fIarg\fR |
| 67 |
.br |
| 68 |
\fB::tcl::mathfunc::sinh\fR \fIarg\fR |
| 69 |
.br |
| 70 |
\fB::tcl::mathfunc::sqrt\fR \fIarg\fR |
| 71 |
.br |
| 72 |
\fB::tcl::mathfunc::srand\fR \fIarg\fR |
| 73 |
.br |
| 74 |
\fB::tcl::mathfunc::tan\fR \fIarg\fR |
| 75 |
.br |
| 76 |
\fB::tcl::mathfunc::tanh\fR \fIarg\fR |
| 77 |
.br |
| 78 |
\fB::tcl::mathfunc::wide\fR \fIarg\fR |
| 79 |
.sp |
| 80 |
.BE |
| 81 |
.SH "DESCRIPTION" |
| 82 |
.PP |
| 83 |
The \fBexpr\fR command handles mathematical functions of the form |
| 84 |
\fBsin($x)\fR or \fBatan2($y,$x)\fR by converting them to calls of the |
| 85 |
form \fB[tcl::mathfunc::sin [expr {$x}]]\fR or |
| 86 |
\fB[tcl::mathfunc::atan2 [expr {$y}] [expr {$x}]]\fR. |
| 87 |
A number of math functions are available by default within the |
| 88 |
namespace \fB::tcl::mathfunc\fR; these functions are also available |
| 89 |
for code apart from \fBexpr\fR, by invoking the given commands |
| 90 |
directly. |
| 91 |
.PP |
| 92 |
Tcl supports the following mathematical functions in expressions, all |
| 93 |
of which work solely with floating-point numbers unless otherwise noted: |
| 94 |
.DS |
| 95 |
.ta 3c 6c 9c |
| 96 |
\fBabs\fR \fBacos\fR \fBasin\fR \fBatan\fR |
| 97 |
\fBatan2\fR \fBbool\fR \fBceil\fR \fBcos\fR |
| 98 |
\fBcosh\fR \fBdouble\fR \fBentier\fR \fBexp\fR |
| 99 |
\fBfloor\fR \fBfmod\fR \fBhypot\fR \fBint\fR |
| 100 |
\fBisqrt\fR \fBlog\fR \fBlog10\fR \fBmax\fR |
| 101 |
\fBmin\fR \fBpow\fR \fBrand\fR \fBround\fR |
| 102 |
\fBsin\fR \fBsinh\fR \fBsqrt\fR \fBsrand\fR |
| 103 |
\fBtan\fR \fBtanh\fR \fBwide\fR |
| 104 |
.DE |
| 105 |
.PP |
| 106 |
In addition to these predefined functions, applications may |
| 107 |
define additional functions by using \fBproc\fR (or any other method, |
| 108 |
such as \fBinterp alias\fR or \fBTcl_CreateObjCommand\fR) to define |
| 109 |
new commands in the \fBtcl::mathfunc\fR namespace. In addition, an |
| 110 |
obsolete interface named \fBTcl_CreateMathFunc\fR() is available to |
| 111 |
extensions that are written in C. The latter interface is not recommended |
| 112 |
for new implementations. |
| 113 |
.SS "DETAILED DEFINITIONS" |
| 114 |
.TP |
| 115 |
\fBabs \fIarg\fR |
| 116 |
. |
| 117 |
Returns the absolute value of \fIarg\fR. \fIArg\fR may be either |
| 118 |
integer or floating-point, and the result is returned in the same form. |
| 119 |
.TP |
| 120 |
\fBacos \fIarg\fR |
| 121 |
. |
| 122 |
Returns the arc cosine of \fIarg\fR, in the range [\fI0\fR,\fIpi\fR] |
| 123 |
radians. \fIArg\fR should be in the range [\fI\-1\fR,\fI1\fR]. |
| 124 |
.TP |
| 125 |
\fBasin \fIarg\fR |
| 126 |
. |
| 127 |
Returns the arc sine of \fIarg\fR, in the range [\fI\-pi/2\fR,\fIpi/2\fR] |
| 128 |
radians. \fIArg\fR should be in the range [\fI\-1\fR,\fI1\fR]. |
| 129 |
.TP |
| 130 |
\fBatan \fIarg\fR |
| 131 |
. |
| 132 |
Returns the arc tangent of \fIarg\fR, in the range [\fI\-pi/2\fR,\fIpi/2\fR] |
| 133 |
radians. |
| 134 |
.TP |
| 135 |
\fBatan2 \fIy x\fR |
| 136 |
. |
| 137 |
Returns the arc tangent of \fIy\fR/\fIx\fR, in the range [\fI\-pi\fR,\fIpi\fR] |
| 138 |
radians. \fIx\fR and \fIy\fR cannot both be 0. If \fIx\fR is greater |
| 139 |
than \fI0\fR, this is equivalent to |
| 140 |
.QW "\fBatan \fR[\fBexpr\fR {\fIy\fB/\fIx\fR}]" . |
| 141 |
.TP |
| 142 |
\fBbool \fIarg\fR |
| 143 |
. |
| 144 |
Accepts any numeric value, or any string acceptable to |
| 145 |
\fBstring is boolean\fR, and returns the corresponding |
| 146 |
boolean value \fB0\fR or \fB1\fR. Non-zero numbers are true. |
| 147 |
Other numbers are false. Non-numeric strings produce boolean value in |
| 148 |
agreement with \fBstring is true\fR and \fBstring is false\fR. |
| 149 |
.TP |
| 150 |
\fBceil \fIarg\fR |
| 151 |
. |
| 152 |
Returns the smallest integral floating-point value (i.e. with a zero |
| 153 |
fractional part) not less than \fIarg\fR. The argument may be any |
| 154 |
numeric value. |
| 155 |
.TP |
| 156 |
\fBcos \fIarg\fR |
| 157 |
. |
| 158 |
Returns the cosine of \fIarg\fR, measured in radians. |
| 159 |
.TP |
| 160 |
\fBcosh \fIarg\fR |
| 161 |
. |
| 162 |
Returns the hyperbolic cosine of \fIarg\fR. If the result would cause |
| 163 |
an overflow, an error is returned. |
| 164 |
.TP |
| 165 |
\fBdouble \fIarg\fR |
| 166 |
. |
| 167 |
The argument may be any numeric value, |
| 168 |
If \fIarg\fR is a floating-point value, returns \fIarg\fR, otherwise converts |
| 169 |
\fIarg\fR to floating-point and returns the converted value. May return |
| 170 |
\fBInf\fR or \fB\-Inf\fR when the argument is a numeric value that exceeds |
| 171 |
the floating-point range. |
| 172 |
.TP |
| 173 |
\fBentier \fIarg\fR |
| 174 |
. |
| 175 |
The argument may be any numeric value. The integer part of \fIarg\fR |
| 176 |
is determined and returned. The integer range returned by this function |
| 177 |
is unlimited, unlike \fBint\fR and \fBwide\fR which |
| 178 |
truncate their range to fit in particular storage widths. |
| 179 |
.TP |
| 180 |
\fBexp \fIarg\fR |
| 181 |
. |
| 182 |
Returns the exponential of \fIarg\fR, defined as \fIe\fR**\fIarg\fR. |
| 183 |
If the result would cause an overflow, an error is returned. |
| 184 |
.TP |
| 185 |
\fBfloor \fIarg\fR |
| 186 |
. |
| 187 |
Returns the largest integral floating-point value (i.e. with a zero |
| 188 |
fractional part) not greater than \fIarg\fR. The argument may be |
| 189 |
any numeric value. |
| 190 |
.TP |
| 191 |
\fBfmod \fIx y\fR |
| 192 |
. |
| 193 |
Returns the floating-point remainder of the division of \fIx\fR by |
| 194 |
\fIy\fR. If \fIy\fR is 0, an error is returned. |
| 195 |
.TP |
| 196 |
\fBhypot \fIx y\fR |
| 197 |
. |
| 198 |
Computes the length of the hypotenuse of a right-angled triangle, |
| 199 |
approximately |
| 200 |
.QW "\fBsqrt\fR [\fBexpr\fR {\fIx\fB*\fIx\fB+\fIy\fB*\fIy\fR}]" |
| 201 |
except for being more numerically stable when the two arguments have |
| 202 |
substantially different magnitudes. |
| 203 |
.TP |
| 204 |
\fBint \fIarg\fR |
| 205 |
. |
| 206 |
The argument may be any numeric value. The integer part of \fIarg\fR |
| 207 |
is determined, and then the low order bits of that integer value up |
| 208 |
to the machine word size are returned as an integer value. For reference, |
| 209 |
the number of bytes in the machine word are stored in the \fBwordSize\fR |
| 210 |
element of the \fBtcl_platform\fR array. |
| 211 |
.TP |
| 212 |
\fBisqrt \fIarg\fR |
| 213 |
. |
| 214 |
Computes the integer part of the square root of \fIarg\fR. \fIArg\fR must be |
| 215 |
a positive value, either an integer or a floating point number. |
| 216 |
Unlike \fBsqrt\fR, which is limited to the precision of a floating point |
| 217 |
number, \fIisqrt\fR will return a result of arbitrary precision. |
| 218 |
.TP |
| 219 |
\fBlog \fIarg\fR |
| 220 |
. |
| 221 |
Returns the natural logarithm of \fIarg\fR. \fIArg\fR must be a |
| 222 |
positive value. |
| 223 |
.TP |
| 224 |
\fBlog10 \fIarg\fR |
| 225 |
. |
| 226 |
Returns the base 10 logarithm of \fIarg\fR. \fIArg\fR must be a |
| 227 |
positive value. |
| 228 |
.TP |
| 229 |
\fBmax \fIarg\fB \fI...\fR |
| 230 |
. |
| 231 |
Accepts one or more numeric arguments. Returns the one argument |
| 232 |
with the greatest value. |
| 233 |
.TP |
| 234 |
\fBmin \fIarg\fB \fI...\fR |
| 235 |
. |
| 236 |
Accepts one or more numeric arguments. Returns the one argument |
| 237 |
with the least value. |
| 238 |
.TP |
| 239 |
\fBpow \fIx y\fR |
| 240 |
. |
| 241 |
Computes the value of \fIx\fR raised to the power \fIy\fR. If \fIx\fR |
| 242 |
is negative, \fIy\fR must be an integer value. |
| 243 |
.TP |
| 244 |
\fBrand\fR |
| 245 |
. |
| 246 |
Returns a pseudo-random floating-point value in the range (\fI0\fR,\fI1\fR). |
| 247 |
The generator algorithm is a simple linear congruential generator that |
| 248 |
is not cryptographically secure. Each result from \fBrand\fR completely |
| 249 |
determines all future results from subsequent calls to \fBrand\fR, so |
| 250 |
\fBrand\fR should not be used to generate a sequence of secrets, such as |
| 251 |
one-time passwords. The seed of the generator is initialized from the |
| 252 |
internal clock of the machine or may be set with the \fBsrand\fR function. |
| 253 |
.TP |
| 254 |
\fBround \fIarg\fR |
| 255 |
. |
| 256 |
If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts |
| 257 |
\fIarg\fR to integer by rounding and returns the converted value. |
| 258 |
.TP |
| 259 |
\fBsin \fIarg\fR |
| 260 |
. |
| 261 |
Returns the sine of \fIarg\fR, measured in radians. |
| 262 |
.TP |
| 263 |
\fBsinh \fIarg\fR |
| 264 |
. |
| 265 |
Returns the hyperbolic sine of \fIarg\fR. If the result would cause |
| 266 |
an overflow, an error is returned. |
| 267 |
.TP |
| 268 |
\fBsqrt \fIarg\fR |
| 269 |
. |
| 270 |
The argument may be any non-negative numeric value. Returns a floating-point |
| 271 |
value that is the square root of \fIarg\fR. May return \fBInf\fR when the |
| 272 |
argument is a numeric value that exceeds the square of the maximum value of |
| 273 |
the floating-point range. |
| 274 |
.TP |
| 275 |
\fBsrand \fIarg\fR |
| 276 |
. |
| 277 |
The \fIarg\fR, which must be an integer, is used to reset the seed for |
| 278 |
the random number generator of \fBrand\fR. Returns the first random |
| 279 |
number (see \fBrand\fR) from that seed. Each interpreter has its own seed. |
| 280 |
.TP |
| 281 |
\fBtan \fIarg\fR |
| 282 |
. |
| 283 |
Returns the tangent of \fIarg\fR, measured in radians. |
| 284 |
.TP |
| 285 |
\fBtanh \fIarg\fR |
| 286 |
. |
| 287 |
Returns the hyperbolic tangent of \fIarg\fR. |
| 288 |
.TP |
| 289 |
\fBwide \fIarg\fR |
| 290 |
. |
| 291 |
The argument may be any numeric value. The integer part of \fIarg\fR |
| 292 |
is determined, and then the low order 64 bits of that integer value |
| 293 |
are returned as an integer value. |
| 294 |
.SH "SEE ALSO" |
| 295 |
expr(n), mathop(n), namespace(n) |
| 296 |
.SH "COPYRIGHT" |
| 297 |
.nf |
| 298 |
Copyright \(co 1993 The Regents of the University of California. |
| 299 |
Copyright \(co 1994-2000 Sun Microsystems Incorporated. |
| 300 |
Copyright \(co 2005, 2006 Kevin B. Kenny <[email protected]>. |
| 301 |
.fi |
| 302 |
'\" Local Variables: |
| 303 |
'\" mode: nroff |
| 304 |
'\" fill-column: 78 |
| 305 |
'\" End: |