[25] | 1 | '\" |
---|
| 2 | '\" Copyright (c) 1989-1993 The Regents of the University of California. |
---|
| 3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
| 4 | '\" |
---|
| 5 | '\" See the file "license.terms" for information on usage and redistribution |
---|
| 6 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 7 | '\" |
---|
| 8 | '\" RCS: @(#) $Id: CrtMathFnc.3,v 1.17 2007/12/13 15:22:30 dgp Exp $ |
---|
| 9 | '\" |
---|
| 10 | .so man.macros |
---|
| 11 | .TH Tcl_CreateMathFunc 3 8.4 Tcl "Tcl Library Procedures" |
---|
| 12 | .BS |
---|
| 13 | .SH NAME |
---|
| 14 | Tcl_CreateMathFunc, Tcl_GetMathFuncInfo, Tcl_ListMathFuncs \- Define, query and enumerate math functions for expressions |
---|
| 15 | .SH SYNOPSIS |
---|
| 16 | .nf |
---|
| 17 | \fB#include <tcl.h>\fR |
---|
| 18 | .sp |
---|
| 19 | void |
---|
| 20 | \fBTcl_CreateMathFunc\fR(\fIinterp, name, numArgs, argTypes, proc, clientData\fR) |
---|
| 21 | .sp |
---|
| 22 | int |
---|
| 23 | \fBTcl_GetMathFuncInfo\fR(\fIinterp, name, numArgsPtr, argTypesPtr, procPtr, |
---|
| 24 | clientDataPtr\fR) |
---|
| 25 | .sp |
---|
| 26 | Tcl_Obj * |
---|
| 27 | \fBTcl_ListMathFuncs\fR(\fIinterp, pattern\fR) |
---|
| 28 | .SH ARGUMENTS |
---|
| 29 | .AS Tcl_ValueType *clientDataPtr out |
---|
| 30 | .AP Tcl_Interp *interp in |
---|
| 31 | Interpreter in which new function will be defined. |
---|
| 32 | .AP "const char" *name in |
---|
| 33 | Name for new function. |
---|
| 34 | .AP int numArgs in |
---|
| 35 | Number of arguments to new function; also gives size of \fIargTypes\fR array. |
---|
| 36 | .AP Tcl_ValueType *argTypes in |
---|
| 37 | Points to an array giving the permissible types for each argument to |
---|
| 38 | function. |
---|
| 39 | .AP Tcl_MathProc *proc in |
---|
| 40 | Procedure that implements the function. |
---|
| 41 | .AP ClientData clientData in |
---|
| 42 | Arbitrary one-word value to pass to \fIproc\fR when it is invoked. |
---|
| 43 | .AP int *numArgsPtr out |
---|
| 44 | Points to a variable that will be set to contain the number of |
---|
| 45 | arguments to the function. |
---|
| 46 | .AP Tcl_ValueType **argTypesPtr out |
---|
| 47 | Points to a variable that will be set to contain a pointer to an array |
---|
| 48 | giving the permissible types for each argument to the function which |
---|
| 49 | will need to be freed up using \fITcl_Free\fR. |
---|
| 50 | .AP Tcl_MathProc **procPtr out |
---|
| 51 | Points to a variable that will be set to contain a pointer to the |
---|
| 52 | implementation code for the function (or NULL if the function is |
---|
| 53 | implemented directly in bytecode). |
---|
| 54 | .AP ClientData *clientDataPtr out |
---|
| 55 | Points to a variable that will be set to contain the clientData |
---|
| 56 | argument passed to \fITcl_CreateMathFunc\fR when the function was |
---|
| 57 | created if the function is not implemented directly in bytecode. |
---|
| 58 | .AP "const char" *pattern in |
---|
| 59 | Pattern to match against function names so as to filter them (by |
---|
| 60 | passing to \fITcl_StringMatch\fR), or NULL to not apply any filter. |
---|
| 61 | .BE |
---|
| 62 | |
---|
| 63 | .SH DESCRIPTION |
---|
| 64 | .PP |
---|
| 65 | Tcl allows a number of mathematical functions to be used in |
---|
| 66 | expressions, such as \fBsin\fR, \fBcos\fR, and \fBhypot\fR. |
---|
| 67 | These functions are represented by commands in the namespace, |
---|
| 68 | \fBtcl::mathfunc\fR. The \fBTcl_CreateMathFunc\fR function is |
---|
| 69 | an obsolete way for applications to add additional functions |
---|
| 70 | to those already provided by Tcl or to replace existing functions. |
---|
| 71 | It should not be used by new applications, which should create |
---|
| 72 | math functions using \fBTcl_CreateObjCommand\fR to create a command |
---|
| 73 | in the \fBtcl::mathfunc\fR namespace. |
---|
| 74 | .PP |
---|
| 75 | In the \fBTcl_CreateMathFunc\fR interface, |
---|
| 76 | \fIName\fR is the name of the function as it will appear in expressions. |
---|
| 77 | If \fIname\fR does not already exist in the \fB::tcl::mathfunc\fR |
---|
| 78 | namespace, then a new command is created in that namespace. |
---|
| 79 | If \fIname\fR does exist, then the existing function is replaced. |
---|
| 80 | \fINumArgs\fR and \fIargTypes\fR describe the arguments to the function. |
---|
| 81 | Each entry in the \fIargTypes\fR array must be |
---|
| 82 | one of \fBTCL_INT\fR, \fBTCL_DOUBLE\fR, \fBTCL_WIDE_INT\fR, |
---|
| 83 | or \fBTCL_EITHER\fR to indicate whether the corresponding argument must be an |
---|
| 84 | integer, a double-precision floating value, a wide (64-bit) integer, |
---|
| 85 | or any, respectively. |
---|
| 86 | .PP |
---|
| 87 | Whenever the function is invoked in an expression Tcl will invoke |
---|
| 88 | \fIproc\fR. \fIProc\fR should have arguments and result that match |
---|
| 89 | the type \fBTcl_MathProc\fR: |
---|
| 90 | .CS |
---|
| 91 | typedef int Tcl_MathProc( |
---|
| 92 | ClientData \fIclientData\fR, |
---|
| 93 | Tcl_Interp *\fIinterp\fR, |
---|
| 94 | Tcl_Value *\fIargs\fR, |
---|
| 95 | Tcl_Value *\fIresultPtr\fR); |
---|
| 96 | .CE |
---|
| 97 | .PP |
---|
| 98 | When \fIproc\fR is invoked the \fIclientData\fR and \fIinterp\fR |
---|
| 99 | arguments will be the same as those passed to \fBTcl_CreateMathFunc\fR. |
---|
| 100 | \fIArgs\fR will point to an array of \fInumArgs\fR Tcl_Value structures, |
---|
| 101 | which describe the actual arguments to the function: |
---|
| 102 | .CS |
---|
| 103 | typedef struct Tcl_Value { |
---|
| 104 | Tcl_ValueType \fItype\fR; |
---|
| 105 | long \fIintValue\fR; |
---|
| 106 | double \fIdoubleValue\fR; |
---|
| 107 | Tcl_WideInt \fIwideValue\fR; |
---|
| 108 | } Tcl_Value; |
---|
| 109 | .CE |
---|
| 110 | .PP |
---|
| 111 | The \fItype\fR field indicates the type of the argument and is |
---|
| 112 | one of \fBTCL_INT\fR, \fBTCL_DOUBLE\fR or \fBTCL_WIDE_INT\fR. |
---|
| 113 | It will match the \fIargTypes\fR value specified for the function unless |
---|
| 114 | the \fIargTypes\fR value was \fBTCL_EITHER\fR. Tcl converts |
---|
| 115 | the argument supplied in the expression to the type requested in |
---|
| 116 | \fIargTypes\fR, if that is necessary. |
---|
| 117 | Depending on the value of the \fItype\fR field, the \fIintValue\fR, |
---|
| 118 | \fIdoubleValue\fR or \fIwideValue\fR |
---|
| 119 | field will contain the actual value of the argument. |
---|
| 120 | .PP |
---|
| 121 | \fIProc\fR should compute its result and store it either as an integer |
---|
| 122 | in \fIresultPtr->intValue\fR or as a floating value in |
---|
| 123 | \fIresultPtr->doubleValue\fR. |
---|
| 124 | It should set also \fIresultPtr->type\fR to one of |
---|
| 125 | \fBTCL_INT\fR, \fBTCL_DOUBLE\fR or \fBTCL_WIDE_INT\fR |
---|
| 126 | to indicate which value was set. |
---|
| 127 | Under normal circumstances \fIproc\fR should return \fBTCL_OK\fR. |
---|
| 128 | If an error occurs while executing the function, \fIproc\fR should |
---|
| 129 | return \fBTCL_ERROR\fR and leave an error message in the interpreter's result. |
---|
| 130 | .PP |
---|
| 131 | \fBTcl_GetMathFuncInfo\fR retrieves the values associated with |
---|
| 132 | function \fIname\fR that were passed to a preceding |
---|
| 133 | \fBTcl_CreateMathFunc\fR call. Normally, the return code is |
---|
| 134 | \fBTCL_OK\fR but if the named function does not exist, \fBTCL_ERROR\fR |
---|
| 135 | is returned and an error message is placed in the interpreter's |
---|
| 136 | result. |
---|
| 137 | .PP |
---|
| 138 | If an error did not occur, the array reference placed in the variable |
---|
| 139 | pointed to by \fIargTypesPtr\fR is newly allocated, and should be |
---|
| 140 | released by passing it to \fBTcl_Free\fR. Some functions (the |
---|
| 141 | standard set implemented in the core, and those defined by placing |
---|
| 142 | commands in the \fBtcl::mathfunc\fR namespace) do not have |
---|
| 143 | argument type information; attempting to retrieve values for |
---|
| 144 | them causes a NULL to be stored in the variable pointed to by |
---|
| 145 | \fIprocPtr\fR and the variable pointed to by \fIclientDataPtr\fR |
---|
| 146 | will not be modified. The variable pointed to by \fInumArgsPointer\fR |
---|
| 147 | will contain -1, and no argument types will be stored in the variable |
---|
| 148 | pointed to by \fIargTypesPointer\fR. |
---|
| 149 | .PP |
---|
| 150 | \fBTcl_ListMathFuncs\fR returns a Tcl object containing a list of all |
---|
| 151 | the math functions defined in the interpreter whose name matches |
---|
| 152 | \fIpattern\fR. The returned object has a reference count of zero. |
---|
| 153 | |
---|
| 154 | .SH "SEE ALSO" |
---|
| 155 | expr(n), info(n), Tcl_CreateObjCommand(3), Tcl_Free(3), Tcl_NewListObj(3) |
---|
| 156 | |
---|
| 157 | .SH KEYWORDS |
---|
| 158 | expression, mathematical function |
---|