mathfunc(n) Tcl Mathematical Functions mathfunc(n)

```

```

# [NAME](#NAME)

mathfunc - Mathematical functions for Tcl expressions

# [SYNOPSIS](#SYNOPSIS)

```
package require Tcl 8.5-
::tcl::mathfunc::abs arg
::tcl::mathfunc::acos arg
::tcl::mathfunc::acosh arg
::tcl::mathfunc::asin arg
::tcl::mathfunc::asin arg
::tcl::mathfunc::atan arg
::tcl::mathfunc::atan2 y x
::tcl::mathfunc::atan arg
::tcl::mathfunc::bool arg
::tcl::mathfunc::cbrt arg
::tcl::mathfunc::ceil arg
::tcl::mathfunc::copysign x y
::tcl::mathfunc::cos arg
::tcl::mathfunc::cosh arg
::tcl::mathfunc::dim x y
::tcl::mathfunc::double arg
::tcl::mathfunc::entier arg
::tcl::mathfunc::erf arg
::tcl::mathfunc::erfc arg
::tcl::mathfunc::exp arg
::tcl::mathfunc::exp2 arg
::tcl::mathfunc::expm1 arg
::tcl::mathfunc::floor arg
::tcl::mathfunc::fma x y z
::tcl::mathfunc::fmod x y
::tcl::mathfunc::hypot x y
::tcl::mathfunc::int arg
::tcl::mathfunc::isfinite arg
::tcl::mathfunc::isinf arg
::tcl::mathfunc::isnan arg
::tcl::mathfunc::isnormal arg
::tcl::mathfunc::isqrt arg
::tcl::mathfunc::issubnormal arg
::tcl::mathfunc::isunordered x y
::tcl::mathfunc::lgamma arg
::tcl::mathfunc::log arg
::tcl::mathfunc::log10 arg
::tcl::mathfunc::log1p arg
::tcl::mathfunc::log2 arg
::tcl::mathfunc::logb arg
::tcl::mathfunc::max arg ?arg ...?
::tcl::mathfunc::min arg ?arg ...?
::tcl::mathfunc::nextafter x y
::tcl::mathfunc::pow x y
::tcl::mathfunc::rand
::tcl::mathfunc::remainder x y
::tcl::mathfunc::round arg
::tcl::mathfunc::signbit arg
::tcl::mathfunc::sin arg
::tcl::mathfunc::sinh arg
::tcl::mathfunc::sqrt arg
::tcl::mathfunc::srand arg
::tcl::mathfunc::tan arg
::tcl::mathfunc::tanh arg
::tcl::mathfunc::gamma arg
::tcl::mathfunc::trunc arg
::tcl::mathfunc::wide arg
```

```

```

# [DESCRIPTION](#DESCRIPTION)

The **expr** command handles mathematical functions of the form **sin($x)** or **atan2($y,$x)** by converting them to calls of the form **\[tcl::mathfunc::sin \[expr {$x}]]** or **\[tcl::mathfunc::atan2 \[expr {$y}] \[expr {$x}]]**. A number of math functions are available by default within the namespace **::tcl::mathfunc**; these functions are also available for code apart from **expr**, 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:

```
abs	acos	acosh	asin
asinh	atan	atan2	atanh
bool	cbrt	copysign	ceil
cos	cosh	dim	double
entier	erf	erfc	exp
exp2	expm1	floor	fma
fmod	gamma	hypot	int
isfinite	isinf	isnan	isnormal
isqrt	issubnormal	isunordered	ldexp
lgamma	log	log10	log1p
log2	logb	max	min
nextafter	pow	rand	remainder
round	sin	sinh	sqrt
srand	tan	tanh	trunc
wide
```

In addition to these predefined functions, applications may define additional functions by using **proc** (or any other method, such as **interp alias** or **Tcl\_CreateObjCommand**) to define new commands in the **tcl::mathfunc** namespace.

## [DETAILED DEFINITIONS](#DETAILED_DEFINITIONS)

[**abs arg**](#abs)

Returns the absolute value of *arg*. *Arg* may be either integer or floating-point, and the result is returned in the same form.

[**acos arg**](#acos)

Returns the arc cosine of *arg*, in the range \[*0*,*pi*] radians. *Arg* should be in the range \[*-1*,*1*].

[**acosh arg**](#acosh)

Returns the arc hyperbolic cosine of *arg*, in the range \[*0*,*inf*] radians. *Arg* should be in the range \[*1*,*inf*].

[**asin arg**](#asin)

Returns the arc sine of *arg*, in the range \[*-pi/2*,*pi/2*] radians. *Arg* should be in the range \[*-1*,*1*].

[**asinh arg**](#asinh)

Returns the arc hyperbolic sine of *arg*, in the range \[*-inf*,*inf*].

[**atan arg**](#atan)

Returns the arc tangent of *arg*, in the range \[*-pi/2*,*pi/2*] radians.

[**atan2 y x**](#atan2)

Returns the arc tangent of *y*/*x*, in the range \[*-pi*,*pi*] radians. *x* and *y* cannot both be 0. If *x* is greater than *0*, this is equivalent to “**atan** \[**expr** {*y/x*}]”.

[**atanh arg**](#atanh)

Returns the arc hyperbolic tangent of *arg*, in the range \[*-inf*,*inf*]. *Arg* should be in the range \[*-1*,*1*].

[**bool arg**](#bool)

Accepts any numeric value, or any string acceptable to **string is boolean**, and returns the corresponding boolean value **0** or **1**. Non-zero numbers are true. Other numbers are false. Non-numeric strings produce boolean value in agreement with **string is true** and **string is false**.

[**cbrt arg**](#cbrt)

The argument may be any numeric value. Returns a floating-point value that is the cube root of *arg*. May return **Inf** when the argument is a numeric value that exceeds the cube of the maximum value of the floating-point range.

[**ceil arg**](#ceil)

Returns the smallest integral floating-point value (i.e. with a zero fractional part) not less than *arg*. The argument may be any numeric value.

[**copysign x y**](#copysign)

Returns the floating-point value with the magnitude of *x* and sign of *y*.

[**cos arg**](#cos)

Returns the cosine of *arg*, measured in radians.

[**cosh arg**](#cosh)

Returns the hyperbolic cosine of *arg*. If the result would cause an overflow, an error is returned.

[**dim x y**](#dim)

Returns the floating-point value that is the maximum of 0 and *x*-*y*.

[**double arg**](#double)

The argument may be any numeric value, If *arg* is a floating-point value, returns *arg*, otherwise converts *arg* to floating-point and returns the converted value. May return **Inf** or **-Inf** when the argument is a numeric value that exceeds the floating-point range.

[**entier arg**](#entier)

The argument may be any numeric value. The integer part of *arg* is determined and returned. The integer range returned by this function is unlimited, unlike **int** and **wide** which truncate their range to fit in particular storage widths.

[**erf arg**](#erf)

Computes the Gauss error function of the numeric value *arg*.

[**erfc arg**](#erfc)

Computes the complementary error function of the numeric value *arg*.

[**exp arg**](#exp)

Returns the exponential of *arg*, defined as *e*\*\**arg*. If the result would cause an overflow, an error is returned.

[**exp2 arg**](#exp2)

Returns the 2 to the power of *arg*, defined as **2**\*\**arg*. The result is always a floating point number except if it would cause an overflow, when an error is returned.

[**expm1 arg**](#expm1)

Returns the exponential of *arg*, minus 1, defined as *e*\*\**arg*-**1**. If the result would cause an overflow, an error is returned. Use of this function can be numerically preferable to of **exp** for values close to 1, and is matched with **log1p**.

[**floor arg**](#floor)

Returns the largest integral floating-point value (i.e. with a zero fractional part) not greater than *arg*. The argument may be any numeric value.

[**fma x y z**](#fma)

Returns the floating-point value that is the sum of the product of *x* and *y*, and *z*, without unnecessarily losing intermediate accuracy.

[**fmod x y**](#fmod)

Returns the floating-point remainder of the division of *x* by *y*. If *y* is 0, an error is returned.

[**gamma arg**](#gamma)

Returns the absolute value of the gamma function of *arg* (a generalized factorial). If the result would cause an overflow, an error is returned.

[**hypot x y**](#hypot)

Computes the length of the hypotenuse of a right-angled triangle, approximately “**sqrt** \[**expr** {*x\*x+y\*y*}]” except for being more numerically stable when the two arguments have substantially different magnitudes.

[**int arg**](#int)

The argument may be any numeric value. The integer part of *arg* 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 **wordSize** element of the **tcl\_platform** array.

[**isfinite arg**](#isfinite)

Returns 1 if the floating-point number *arg* is finite. That is, if it is zero, subnormal, or normal. Returns 0 if the number is infinite or NaN. Throws an error if *arg* cannot be promoted to a floating-point value.

[**isinf arg**](#isinf)

Returns 1 if the floating-point number *arg* is infinite. Returns 0 if the number is finite or NaN. Throws an error if *arg* cannot be promoted to a floating-point value.

[**isnan arg**](#isnan)

Returns 1 if the floating-point number *arg* is Not-a-Number. Returns 0 if the number is finite or infinite. Throws an error if *arg* cannot be promoted to a floating-point value.

[**isnormal arg**](#isnormal)

Returns 1 if the floating-point number *arg* is normal. Returns 0 if the number is zero, subnormal, infinite or NaN. Throws an error if *arg* cannot be promoted to a floating-point value.

[**isqrt arg**](#isqrt)

Computes the integer part of the square root of *arg*. *Arg* must be a positive value, either an integer or a floating point number. Unlike **sqrt**, which is limited to the precision of a floating point number, *isqrt* will return a result of arbitrary precision.

[**issubnormal arg**](#issubnormal)

Returns 1 if the floating-point number *arg* is subnormal, i.e., the result of gradual underflow. Returns 0 if the number is zero, normal, infinite or NaN. Throws an error if *arg* cannot be promoted to a floating-point value.

[**isunordered x y**](#isunordered)

Returns 1 if *x* and *y* 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 *x* or *y* cannot be promoted to a floating-point value.

[**ldexp x y**](#ldexp)

Multiplies a floating-point value *x* by the number 2 raised to the *y* power.

[**lgamma arg**](#lgamma)

Returns the natural logarithm of the absolute value of the gamma function of *arg*. If the result would cause an overflow, an error is returned.

[**log arg**](#log)

Returns the natural logarithm of *arg*. *Arg* must be a positive value.

[**log10 arg**](#log10)

Returns the base 10 logarithm of *arg*. *Arg* must be a positive value.

[**log1p arg**](#log1p)

Returns the natural logarithm of *arg*+**1**. *Arg* must be a value greater than -1. This function is numerically preferable to **log** for values very close to 1, and is matched with **expm1**.

[**log2 arg**](#log2)

Returns the base 2 logarithm of *arg*. *Arg* must be a positive value.

[**logb arg**](#logb)

Returns the value of the unbiased radix-independent exponent from the floating-point argument *arg*.

[**max arg** ...](#max)

Accepts one or more numeric arguments. Returns the one argument with the greatest value.

[**min arg** ...](#min)

Accepts one or more numeric arguments. Returns the one argument with the least value.

[**nextafter x y**](#nextafter)

Returns the next floating-point value after (or before) *x* in the direction of *y*. If *x* and *y* are equal, returns *y*.

[**pow x y**](#pow)

Computes the value of *x* raised to the power *y*. If *x* is negative, *y* must be an integer value.

[**rand**](#rand)

Returns a pseudo-random floating-point value in the range (*0*,*1*). The generator algorithm is a simple linear congruential generator that is not cryptographically secure. Each result from **rand** completely determines all future results from subsequent calls to **rand**, so **rand** 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 **srand** function.

[**remainder x y**](#remainder)

Computes the IEEE remainder of the floating point division operation *x*/*y*.

[**round arg**](#round)

If *arg* is an integer value, returns *arg*, otherwise converts *arg* to integer by rounding and returns the converted value.

[**signbit arg**](#signbit)

Get the sign bit of the numeric value, *arg*, as 0 (positive) or 1 (negative).

[**sin arg**](#sin)

Returns the sine of *arg*, measured in radians.

[**sinh arg**](#sinh)

Returns the hyperbolic sine of *arg*. If the result would cause an overflow, an error is returned.

[**sqrt arg**](#sqrt)

The argument may be any non-negative numeric value. Returns a floating-point value that is the square root of *arg*. May return **Inf** when the argument is a numeric value that exceeds the square of the maximum value of the floating-point range.

[**srand arg**](#srand)

The *arg*, which must be an integer, is used to reset the seed for the random number generator of **rand**. Returns the first random number (see **rand**) from that seed. Each interpreter has its own seed.

[**tan arg**](#tan)

Returns the tangent of *arg*, measured in radians.

[**tanh arg**](#tanh)

Returns the hyperbolic tangent of *arg*.

[**trunc arg**](#trunc)

Returns the nearest integer not greater in magnitude than *arg*.

[**wide arg**](#wide)

The argument may be any numeric value. The integer part of *arg* is determined, and then the low order 64 bits of that integer value are returned as an integer value.

# [SEE ALSO](#SEE_ALSO)

[divmod(n)](/man/os/tcl/divmod.n), [expr(n)](/man/os/tcl/expr.n), [fpclassify(n)](/man/os/tcl/fpclassify.n), [frexp(n)](/man/os/tcl/frexp.n), [mathop(n)](/man/os/tcl/mathop.n), [modf(n)](/man/os/tcl/modf.n), [namespace(n)](/man/os/tcl/namespace.n), [remquo(n)](/man/os/tcl/remquo.n)

# [COPYRIGHT](#COPYRIGHT)

```
Copyright © 1993 The Regents of the University of California.
Copyright © 1994-2000 Sun Microsystems Incorporated.
Copyright © 2005-2006 Kevin B. Kenny <kennykb@acm.org>.
```

8.5 Tcl

---

Document: mathfunc(n) from tcl 9.1.0-r1 (Chainguard OS)
Source: https://downloads.sourceforge.net/project/tcl/Tcl/9.1.0/tcl9.1.0-src.tar.gz
