mathfunc(3tcl)

โœจ NAME โœจ

mathfunc โ€” mathematical functions for Tcl expressions ๐Ÿงฎ๐Ÿ’–


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

package require Tcl 8.5-
::tcl::mathfunc::abs arg
::tcl::mathfunc::acos arg
::tcl::mathfunc::asin arg
::tcl::mathfunc::atan arg
::tcl::mathfunc::atan2 y x
::tcl::mathfunc::bool arg
::tcl::mathfunc::ceil arg
::tcl::mathfunc::cos arg
::tcl::mathfunc::cosh arg
::tcl::mathfunc::double arg
::tcl::mathfunc::entier arg
::tcl::mathfunc::exp arg
::tcl::mathfunc::floor arg
::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::log arg
::tcl::mathfunc::log10 arg
::tcl::mathfunc::max arg ?arg ...?
::tcl::mathfunc::min arg ?arg ...?
::tcl::mathfunc::pow x y
::tcl::mathfunc::rand
::tcl::mathfunc::round 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::wide arg

๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so bestie, here's the tea โ˜• โ€” expr is out here doing math with functions like sin($x) or atan2($y,$x), and the way it handles that is by quietly rewriting them into calls that look like [tcl::mathfunc::sin [expr {$x}]] or [tcl::mathfunc::atan2 [expr {$y}] [expr {$x}]]. iconic behaviour, honestly.

a whole bunch of math functions just come with the namespace ::tcl::mathfunc by default, and the best part? they're not locked inside expr โ€” you can call these commands directly from your own code too. slay. ๐Ÿ’…

so Tcl supports these math functions in expressions, and heads up: they all work solely with floating-point numbers unless the description says otherwise:

abs, acos, asin, atan, atan2, bool, ceil, cos, cosh, double, entier, exp, floor, fmod, hypot, int, isfinite, isinf, isnan, isnormal, isqrt, issubnormal, isunordered, log, log10, max, min, pow, rand, round, sin, sinh, sqrt, srand, tan, tanh, wide

and on top of these pre-made ones, your app can literally just define its own functions ๐Ÿ’– โ€” use proc (or any other method, like interp alias or Tcl_CreateObjCommand) to make new commands inside the tcl::mathfunc namespace. main character energy, fr fr.

๐Ÿ“– DETAILED DEFINITIONS ๐Ÿ“–

  • abs arg โ€” gives you the absolute value of arg. arg can be an integer or a floating-point value, and you get the result back in the same form you put in. ๐Ÿชž
  • acos arg โ€” the arc cosine of arg, landing in the range [0,pi] radians. arg should be in the range [-1,1], bestie.
  • asin arg โ€” arc sine of arg, in the range [-pi/2,pi/2] radians. arg should live in [-1,1].
  • atan arg โ€” arc tangent of arg, in the range [-pi/2,pi/2] radians.
  • atan2 y x โ€” 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}]โ€.
  • bool arg โ€” takes any numeric value, or any string that string is boolean would accept, and hands back the matching boolean value 0 or 1. non-zero numbers are true. other numbers are false. non-numeric strings produce a boolean value in agreement with string is true and string is false. โœ…โŒ
  • ceil arg โ€” the smallest integral floating-point value (meaning a zero fractional part, iykyk) that is not less than arg. the argument can be any numeric value. โฌ†๏ธ
  • cos arg โ€” cosine of arg, measured in radians.
  • cosh arg โ€” hyperbolic cosine of arg. ๐Ÿšจ if the result would cause an overflow, an error is returned.
  • double arg โ€” the argument can be any numeric value. if arg is already a floating-point value, you get arg back; otherwise arg gets converted to floating-point and you get the converted value. it may return Inf or -Inf when the argument is a numeric value that exceeds the floating-point range. ๐ŸŒŠ
  • entier arg โ€” the argument can be any numeric value. the integer part of arg is figured out and returned. and here's the glow-up โœจ: the integer range returned by this function is unlimited, unlike int and wide, which truncate their range to fit into particular storage widths.
  • exp arg โ€” the exponential of arg, defined as e**arg. ๐Ÿšจ if the result would cause an overflow, an error is returned.
  • floor arg โ€” the largest integral floating-point value (zero fractional part, again) that is not greater than arg. argument can be any numeric value. โฌ‡๏ธ
  • fmod x y โ€” the floating-point remainder of dividing x by y. โš ๏ธ if y is 0, an error is returned.
  • hypot x y โ€” computes the length of the hypotenuse of a right-angled triangle, approximately โ€œsqrt [expr {x*x+y*y}]โ€ โ€” except it's more numerically stable when the two arguments have substantially different magnitudes. ๐Ÿ“
  • int arg โ€” the argument can 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 is stored in the wordSize element of the tcl_platform array. ๐Ÿ“
  • isfinite arg โ€” returns 1 if the floating-point number arg is finite โ€” that means 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 โ€” 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 โ€” 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 โ€” 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 โ€” computes the integer part of the square root of arg. arg must be a positive value, either an integer or a floating point number. and unlike sqrt, which is limited to the precision of a floating point number, isqrt will return a result of arbitrary precision. ๐Ÿ”ฅ
  • issubnormal arg โ€” 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 โ€” 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.
  • log arg โ€” the natural logarithm of arg. arg must be a positive value. ๐ŸŒฒ
  • log10 arg โ€” the base 10 logarithm of arg. arg must be a positive value.
  • max arg ... โ€” takes one or more numeric arguments and returns the one argument with the greatest value. ๐Ÿ†
  • min arg ... โ€” takes one or more numeric arguments and returns the one argument with the least value. ๐Ÿฅบ
  • pow x y โ€” computes the value of x raised to the power y. โš ๏ธ if x is negative, y must be an integer value.
  • rand โ€” returns a pseudo-random floating-point value in the range (0,1). ๐ŸŽฒ real talk though: 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 it may be set with the srand function.
  • round arg โ€” if arg is an integer value, you just get arg back; otherwise arg gets converted to integer by rounding, and you get the converted value. ๐ŸŽฏ
  • sin arg โ€” sine of arg, measured in radians.
  • sinh arg โ€” hyperbolic sine of arg. ๐Ÿšจ if the result would cause an overflow, an error is returned.
  • sqrt arg โ€” the argument may be any non-negative numeric value. returns a floating-point value that is the square root of arg. it 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 โ€” 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. and fun fact ๐Ÿซถ: each interpreter has its own seed.
  • tan arg โ€” tangent of arg, measured in radians.
  • tanh arg โ€” hyperbolic tangent of arg.
  • wide arg โ€” 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 ๐Ÿ‘€

expr(3tcl), fpclassify(3tcl), mathop(3tcl), namespace(3tcl)


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

and that's the whole page, bestie โ€” this one's from Tcl 8.5, filed under mathfunc(3tcl) ๐ŸŒˆ๐Ÿ’– go forth and calculate, you absolute math icon ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from
  • ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms