Artifact 1f1f303ab211a1167d65c630eb2909f6047ddac377b1f9276f65e7321c1bb763:
- File doc/markdown/mathfunc.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 11049) [annotate] [blame] [check-ins using]
CommandName: mathfunc ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Mathematical Functions Links:
- expr(n)
- fpclassify(n)
- mathop(n)
- namespace(n) Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-2000 Sun Microsystems, Inc.
- Copyright (c) 2005 Kevin B. Kenny [email protected]. All rights reserved ---
Name
mathfunc - Mathematical functions for Tcl expressions
Synopsis
::: {.synopsis} ::: [package]{.cmd} [require]{.sub} [Tcl]{.lit} [8.5-]{.lit}
[::tcl::mathfunc::abs]{.cmd} [arg]{.arg} [::tcl::mathfunc::acos]{.cmd} [arg]{.arg} [::tcl::mathfunc::asin]{.cmd} [arg]{.arg} [::tcl::mathfunc::atan]{.cmd} [arg]{.arg} [::tcl::mathfunc::atan2]{.cmd} [y]{.arg} [x]{.arg} [::tcl::mathfunc::bool]{.cmd} [arg]{.arg} [::tcl::mathfunc::ceil]{.cmd} [arg]{.arg} [::tcl::mathfunc::cos]{.cmd} [arg]{.arg} [::tcl::mathfunc::cosh]{.cmd} [arg]{.arg} [::tcl::mathfunc::double]{.cmd} [arg]{.arg} [::tcl::mathfunc::entier]{.cmd} [arg]{.arg} [::tcl::mathfunc::exp]{.cmd} [arg]{.arg} [::tcl::mathfunc::floor]{.cmd} [arg]{.arg} [::tcl::mathfunc::fmod]{.cmd} [x]{.arg} [y]{.arg} [::tcl::mathfunc::hypot]{.cmd} [x]{.arg} [y]{.arg} [::tcl::mathfunc::int]{.cmd} [arg]{.arg} [::tcl::mathfunc::isfinite]{.cmd} [arg]{.arg} [::tcl::mathfunc::isinf]{.cmd} [arg]{.arg} [::tcl::mathfunc::isnan]{.cmd} [arg]{.arg} [::tcl::mathfunc::isnormal]{.cmd} [arg]{.arg} [::tcl::mathfunc::isqrt]{.cmd} [arg]{.arg} [::tcl::mathfunc::issubnormal]{.cmd} [arg]{.arg} [::tcl::mathfunc::isunordered]{.cmd} [x]{.arg} [y]{.arg} [::tcl::mathfunc::log]{.cmd} [arg]{.arg} [::tcl::mathfunc::log10]{.cmd} [arg]{.arg} [::tcl::mathfunc::max]{.cmd} [arg]{.arg} [arg]{.optdot} [::tcl::mathfunc::min]{.cmd} [arg]{.arg} [arg]{.optdot} [::tcl::mathfunc::pow]{.cmd} [x]{.arg} [y]{.arg} [::tcl::mathfunc::rand]{.cmd} [::tcl::mathfunc::round]{.cmd} [arg]{.arg} [::tcl::mathfunc::sin]{.cmd} [arg]{.arg} [::tcl::mathfunc::sinh]{.cmd} [arg]{.arg} [::tcl::mathfunc::sqrt]{.cmd} [arg]{.arg} [::tcl::mathfunc::srand]{.cmd} [arg]{.arg} [::tcl::mathfunc::tan]{.cmd} [arg]{.arg} [::tcl::mathfunc::tanh]{.cmd} [arg]{.arg} [::tcl::mathfunc::wide]{.cmd} [arg]{.arg} :::
Description
The fBexprfR command handles mathematical functions of the form fBsin($x)fR or fBatan2($y,$x)fR by converting them to calls of the form fB[tcl::mathfunc::sin [expr {$x}]]fR or fB[tcl::mathfunc::atan2 [expr {$y}] [expr {$x}]]fR. A number of math functions are available by default within the namespace fB::tcl::mathfuncfR; these functions are also available for code apart from fBexprfR, by invoking the given commands directly.
Tcl supports the following mathematical functions in expressions, all of which work solely with floating-point numbers unless otherwise noted: .DS fBabsfR fBacosfR fBasinfR fBatanfR fBatan2fR fBboolfR fBceilfR fBcosfR fBcoshfR fBdoublefR fBentierfR fBexpfR fBfloorfR fBfmodfR fBhypotfR fBintfR fBisfinitefR fBisinffR fBisnanfR fBisnormalfR fBisqrtfR fBissubnormalfR fBisunorderedfR fBlogfR fBlog10fR fBmaxfR fBminfR fBpowfR fBrandfR fBroundfR fBsinfR fBsinhfR fBsqrtfR fBsrandfR fBtanfR fBtanhfR fBwidefR .DE
In addition to these predefined functions, applications may define additional functions by using fBprocfR (or any other method, such as fBinterp aliasfR or fBTcl_CreateObjCommandfR) to define new commands in the fBtcl::mathfuncfR namespace.
Detailed definitions
abs fIargfR : Returns the absolute value of fIargfR. fIArgfR may be either integer or floating-point, and the result is returned in the same form.
acos fIargfR : Returns the arc cosine of fIargfR, in the range [fI0fR,fIpifR] radians. fIArgfR should be in the range [fI-1fR,fI1fR].
asin fIargfR : Returns the arc sine of fIargfR, in the range [fI-pi/2fR,fIpi/2fR] radians. fIArgfR should be in the range [fI-1fR,fI1fR].
atan fIargfR : Returns the arc tangent of fIargfR, in the range [fI-pi/2fR,fIpi/2fR] radians.
atan2 fIy xfR : Returns the arc tangent of fIyfR/fIxfR, in the range [fI-pifR,fIpifR] radians. fIxfR and fIyfR cannot both be 0. If fIxfR is greater than fI0fR, this is equivalent to "atan [fBexprfR {fIyfRfB/fRfIxfR}]".
bool fIargfR : Accepts any numeric value, or any string acceptable to fBstring is booleanfR, and returns the corresponding boolean value fB0fR or fB1fR. Non-zero numbers are true. Other numbers are false. Non-numeric strings produce boolean value in agreement with fBstring is truefR and fBstring is falsefR.
ceil fIargfR : Returns the smallest integral floating-point value (i.e. with a zero fractional part) not less than fIargfR. The argument may be any numeric value.
cos fIargfR : Returns the cosine of fIargfR, measured in radians.
cosh fIargfR : Returns the hyperbolic cosine of fIargfR. If the result would cause an overflow, an error is returned.
double fIargfR : The argument may be any numeric value, If fIargfR is a floating-point value, returns fIargfR, otherwise converts fIargfR to floating-point and returns the converted value. May return fBInffR or fB-InffR when the argument is a numeric value that exceeds the floating-point range.
entier fIargfR : The argument may be any numeric value. The integer part of fIargfR is determined and returned. The integer range returned by this function is unlimited, unlike fBintfR and fBwidefR which truncate their range to fit in particular storage widths.
exp fIargfR : Returns the exponential of fIargfR, defined as fIefR**fIargfR. If the result would cause an overflow, an error is returned.
floor fIargfR : Returns the largest integral floating-point value (i.e. with a zero fractional part) not greater than fIargfR. The argument may be any numeric value.
fmod fIx yfR : Returns the floating-point remainder of the division of fIxfR by fIyfR. If fIyfR is 0, an error is returned.
hypot fIx yfR : Computes the length of the hypotenuse of a right-angled triangle, approximately "sqrt [fBexprfR {fIxfRfB*fRfIxfRfB+fRfIyfRfB*fRfIyfR}]" except for being more numerically stable when the two arguments have substantially different magnitudes.
int fIargfR : The argument may be any numeric value. The integer part of fIargfR is determined, and then the low order bits of that integer value up to the machine word size are returned as an integer value. For reference, the number of bytes in the machine word are stored in the fBwordSizefR element of the fBtcl_platformfR array.
isfinite fIargfR : Returns 1 if the floating-point number fIargfR is finite. That is, if it is zero, subnormal, or normal. Returns 0 if the number is infinite or NaN. Throws an error if fIargfR cannot be promoted to a floating-point value.
isinf fIargfR : Returns 1 if the floating-point number fIargfR is infinite. Returns 0 if the number is finite or NaN. Throws an error if fIargfR cannot be promoted to a floating-point value.
isnan fIargfR : Returns 1 if the floating-point number fIargfR is Not-a-Number. Returns 0 if the number is finite or infinite. Throws an error if fIargfR cannot be promoted to a floating-point value.
isnormal fIargfR : Returns 1 if the floating-point number fIargfR is normal. Returns 0 if the number is zero, subnormal, infinite or NaN. Throws an error if fIargfR cannot be promoted to a floating-point value.
isqrt fIargfR : Computes the integer part of the square root of fIargfR. fIArgfR must be a positive value, either an integer or a floating point number. Unlike fBsqrtfR, which is limited to the precision of a floating point number, fIisqrtfR will return a result of arbitrary precision.
issubnormal fIargfR : Returns 1 if the floating-point number fIargfR is subnormal, i.e., the result of gradual underflow. Returns 0 if the number is zero, normal, infinite or NaN. Throws an error if fIargfR cannot be promoted to a floating-point value.
isunordered fIx yfR : Returns 1 if fIxfR and fIyfR cannot be compared for ordering, that is, if either one is NaN. Returns 0 if both values can be ordered, that is, if they are both chosen from among the set of zero, subnormal, normal and infinite values. Throws an error if either fIxfR or fIyfR cannot be promoted to a floating-point value.
log fIargfR : Returns the natural logarithm of fIargfR. fIArgfR must be a positive value.
log10 fIargfR : Returns the base 10 logarithm of fIargfR. fIArgfR must be a positive value.
max fIargfR fI...fR : Accepts one or more numeric arguments. Returns the one argument with the greatest value.
min fIargfR fI...fR : Accepts one or more numeric arguments. Returns the one argument with the least value.
pow fIx yfR : Computes the value of fIxfR raised to the power fIyfR. If fIxfR is negative, fIyfR must be an integer value.
rand : Returns a pseudo-random floating-point value in the range (fI0fR,fI1fR). The generator algorithm is a simple linear congruential generator that is not cryptographically secure. Each result from fBrandfR completely determines all future results from subsequent calls to fBrandfR, so fBrandfR should not be used to generate a sequence of secrets, such as one-time passwords. The seed of the generator is initialized from the internal clock of the machine or may be set with the fBsrandfR function.
round fIargfR : If fIargfR is an integer value, returns fIargfR, otherwise converts fIargfR to integer by rounding and returns the converted value.
sin fIargfR : Returns the sine of fIargfR, measured in radians.
sinh fIargfR : Returns the hyperbolic sine of fIargfR. If the result would cause an overflow, an error is returned.
sqrt fIargfR : The argument may be any non-negative numeric value. Returns a floating-point value that is the square root of fIargfR. May return fBInffR when the argument is a numeric value that exceeds the square of the maximum value of the floating-point range.
srand fIargfR : The fIargfR, which must be an integer, is used to reset the seed for the random number generator of fBrandfR. Returns the first random number (see fBrandfR) from that seed. Each interpreter has its own seed.
tan fIargfR : Returns the tangent of fIargfR, measured in radians.
tanh fIargfR : Returns the hyperbolic tangent of fIargfR.
wide fIargfR : The argument may be any numeric value. The integer part of fIargfR is determined, and then the low order 64 bits of that integer value are returned as an integer value.
Copyright
Copyright (co 1993 The Regents of the University of California. Copyright (co 1994-2000 Sun Microsystems Incorporated. Copyright (co 2005-2006 Kevin B. Kenny [email protected].