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 the argTypes array, 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 to proc when 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 using Tcl_Free.
  • Tcl_MathProc **procPtr (out) β€” points to a variable that will be set to contain a pointer to the implementation code for the function, or NULL if the function is implemented directly in bytecode.
  • ClientData *clientDataPtr (out) β€” points to a variable that will be set to contain the clientData argument passed to Tcl_CreateMathFunc when 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 to Tcl_StringMatch), or NULL to 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