Tcl_CreateMathFunc(3tcl)
π Tcl_CreateMathFunc β the math-function glow-up page π
okay so bestie, welcome to Tcl_CreateMathFunc(3tcl), straight out of the Tcl Library Procedures collection. this is the page about defining, querying and enumerating math functions for expressions. yes it's a lot. no we're not skipping any of it. grab a boba π§ and let's go.
β¨ NAME β¨
Tcl_CreateMathFunc, Tcl_GetMathFuncInfo, Tcl_ListMathFuncs β Define, query and enumerate math functions for expressions.
π¨ NOTICE OF EVENTUAL DEPRECATION π¨
not to be dramatic but Tcl_CreateMathFunc and Tcl_GetMathFuncInfo are somewhat obsolete now, because you can just make functions for expressions by dropping commands into the tcl::mathfunc namespace β that's all described over in the mathfunc manual page. and like, the API on this page? it is not expected to be maintained indefinitely. so it's giving "still works, still iconic, but don't build your whole life on it" π
β¨ SYNOPSIS β¨
#include <tcl.h>
void
Tcl_CreateMathFunc(interp, name, numArgs, argTypes, proc, clientData)
int
Tcl_GetMathFuncInfo(interp, name, numArgsPtr, argTypesPtr, procPtr,
clientDataPtr)
Tcl_Obj *
Tcl_ListMathFuncs(interp, pattern)
π ARGUMENTS π
Tcl_Interp *interp(in) β the interpreter where the new function is gonna get defined. that's her home now πconst char *name(in) β the name for the new function.int numArgs(in) β how many arguments the new function takes. also doubles as the size of theargTypesarray, multitasking queen πTcl_ValueType *argTypes(in) β points to an array giving the permissible types for each argument to the function.Tcl_MathProc *proc(in) β the procedure that actually implements the function.ClientData clientData(in) β an arbitrary one-word value that gets passed toprocwhen it's invoked. little surprise gift πint *numArgsPtr(out) β points to a variable that will be set to contain the number of arguments to the function.Tcl_ValueType **argTypesPtr(out) β points to a variable that will be set to contain a pointer to an array giving the permissible types for each argument to the function, which will need to be freed up usingTcl_Free.Tcl_MathProc **procPtr(out) β points to a variable that will be set to contain a pointer to the implementation code for the function, orNULLif the function is implemented directly in bytecode.ClientData *clientDataPtr(out) β points to a variable that will be set to contain theclientDataargument passed toTcl_CreateMathFuncwhen the function was created, if the function is not implemented directly in bytecode.const char *pattern(in) β a pattern to match against function names so you can filter them (it gets passed toTcl_StringMatch), orNULLto not apply any filter at all. choose your own adventure π
π DESCRIPTION π
Tcl lets you use a bunch of mathematical functions inside expressions β like sin, cos, and hypot, the classics. these functions are represented by commands in the namespace, tcl::mathfunc.
Tcl_CreateMathFunc is an obsolete way for applications to add extra functions on top of the ones Tcl already gives you, or to replace functions that already exist. so basically: new applications should not use it. if you're new here, you should create math functions using Tcl_CreateObjCommand to create a command in the tcl::mathfunc namespace instead. it's giving "the old way still works but the new way ate" π
okay so in the Tcl_CreateMathFunc interface, Name is the name of the function as it will appear in expressions. if name doesn't already exist in the ::tcl::mathfunc namespace, then a new command gets created in that namespace. if name does already exist, then the existing function gets replaced. no hesitation, no negotiation π
NumArgs and argTypes describe the arguments to the function. every entry in the argTypes array has to be one of TCL_INT, TCL_DOUBLE, TCL_WIDE_INT, or TCL_EITHER β saying whether the corresponding argument must be an integer, a double-precision floating value, a wide (64-bit) integer, or any of them respectively.
whenever the function gets invoked in an expression, Tcl will invoke proc. proc should have arguments and a result that match the type Tcl_MathProc:
typedef int Tcl_MathProc(
ClientData clientData,
Tcl_Interp *interp,
Tcl_Value *args,
Tcl_Value *resultPtr);
when proc is invoked, the clientData and interp arguments will be the same as the ones you passed to Tcl_CreateMathFunc. and args will point to an array of numArgs Tcl_Value structures, which describe the actual arguments to the function:
typedef struct Tcl_Value {
Tcl_ValueType type;
long intValue;
double doubleValue;
Tcl_WideInt wideValue;
} Tcl_Value;
the type field tells you the type of the argument, and it's one of TCL_INT, TCL_DOUBLE or TCL_WIDE_INT. it will match the argTypes value you specified for the function β unless that argTypes value was TCL_EITHER. Tcl converts the argument supplied in the expression to the type requested in argTypes, if that's necessary. depending on what the type field says, the intValue, doubleValue or wideValue field will hold the actual value of the argument. pick your fighter π₯
Proc should compute its result and store it either as an integer in resultPtr->intValue or as a floating value in resultPtr->doubleValue. it should also set resultPtr->type to one of TCL_INT, TCL_DOUBLE or TCL_WIDE_INT to indicate which value was set. under normal circumstances proc should return TCL_OK. and if an error happens while executing the function, proc should return TCL_ERROR and leave an error message in the interpreter's result. don't forget the error message, bestie, that's the whole point. π
π Tcl_GetMathFuncInfo π
Tcl_GetMathFuncInfo retrieves the values associated with function name that were passed to a preceding Tcl_CreateMathFunc call. normally the return code is TCL_OK, but if the named function doesn't exist, TCL_ERROR is returned and an error message is placed in the interpreter's result. gone girl, she's not there. π
if an error did not occur, the array reference placed in the variable pointed to by argTypesPtr is newly allocated, and should be released by passing it to Tcl_Free. β yes, you have to clean up after yourself, that's the vibe π§Ή
some functions (the standard set implemented in the core, and those defined by placing commands in the tcl::mathfunc namespace) do not have argument type information. attempting to retrieve values for them causes a NULL to be stored in the variable pointed to by procPtr, and the variable pointed to by clientDataPtr will not be modified. the variable pointed to by numArgsPointer will contain -1, and no argument types will be stored in the variable pointed to by argTypesPointer. so like... heads up, that's a whole different shape of answer π
π Tcl_ListMathFuncs π
Tcl_ListMathFuncs returns a Tcl value containing a list of all the math functions defined in the interpreter whose name matches pattern. and the returned value has a reference count of zero. noted. important. remembered. β
π SEE ALSO π
expr(3tcl), info(3tcl), Tcl_CreateObjCommand(3tcl), Tcl_Free(3tcl), Tcl_NewListObj(3tcl)
π KEYWORDS π
expression, mathematical function
and that's the whole page, no cap. this one's from Tcl 8.4 and it's the Tcl_CreateMathFunc(3tcl) page β go forth and math, bestie π
β¨π¦
π¦ where this page comes from
- debian bookworm: tcl8.6-doc 8.6.13+dfsg-2
- debian trixie: tcl8.6-doc 8.6.16+dfsg-1
- ubuntu jammy: tcl8.6-doc 8.6.12+dfsg-1build1
- ubuntu noble: tcl8.6-doc 8.6.14+dfsg-1build1
- ubuntu questing: tcl8.6-doc 8.6.17+dfsg-1
- ubuntu resolute: tcl8.6-doc 8.6.17+dfsg-1build1
- ubuntu stonking: tcl8.6-doc 8.6.18+dfsg-1
- Copyright (c) 1989-1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms