1mathfunc(n) Tcl Mathematical Functions mathfunc(n)
2
3
4
5______________________________________________________________________________
6
8 mathfunc - Mathematical functions for Tcl expressions
9
11 package require Tcl 8.5
12
13 ::tcl::mathfunc::abs arg
14 ::tcl::mathfunc::acos arg
15 ::tcl::mathfunc::asin arg
16 ::tcl::mathfunc::atan arg
17 ::tcl::mathfunc::atan2 y x
18 ::tcl::mathfunc::bool arg
19 ::tcl::mathfunc::ceil arg
20 ::tcl::mathfunc::cos arg
21 ::tcl::mathfunc::cosh arg
22 ::tcl::mathfunc::double arg
23 ::tcl::mathfunc::entier arg
24 ::tcl::mathfunc::exp arg
25 ::tcl::mathfunc::floor arg
26 ::tcl::mathfunc::fmod x y
27 ::tcl::mathfunc::hypot x y
28 ::tcl::mathfunc::int arg
29 ::tcl::mathfunc::isqrt arg
30 ::tcl::mathfunc::log arg
31 ::tcl::mathfunc::log10 arg
32 ::tcl::mathfunc::max arg ?arg ...?
33 ::tcl::mathfunc::min arg ?arg ...?
34 ::tcl::mathfunc::pow x y
35 ::tcl::mathfunc::rand
36 ::tcl::mathfunc::round arg
37 ::tcl::mathfunc::sin arg
38 ::tcl::mathfunc::sinh arg
39 ::tcl::mathfunc::sqrt arg
40 ::tcl::mathfunc::srand arg
41 ::tcl::mathfunc::tan arg
42 ::tcl::mathfunc::tanh arg
43 ::tcl::mathfunc::wide arg
44
45______________________________________________________________________________
46
48 The expr command handles mathematical functions of the form sin($x) or
49 atan2($y,$x) by converting them to calls of the form [tcl::math‐
50 func::sin [expr {$x}]] or [tcl::mathfunc::atan2 [expr {$y}] [expr
51 {$x}]]. A number of math functions are available by default within the
52 namespace ::tcl::mathfunc; these functions are also available for code
53 apart from expr, by invoking the given commands directly.
54
55 Tcl supports the following mathematical functions in expressions, all
56 of which work solely with floating-point numbers unless otherwise
57 noted:
58
59 abs acos asin atan
60 atan2 bool ceil cos
61 cosh double entier exp
62 floor fmod hypot int
63 isqrt log log10 max
64 min pow rand round
65 sin sinh sqrt srand
66 tan tanh wide
67
68
69 In addition to these predefined functions, applications may define
70 additional functions by using proc (or any other method, such as interp
71 alias or Tcl_CreateObjCommand) to define new commands in the tcl::math‐
72 func namespace. In addition, an obsolete interface named Tcl_Cre‐
73 ateMathFunc() is available to extensions that are written in C. The
74 latter interface is not recommended for new implementations.
75
76 DETAILED DEFINITIONS
77 abs arg
78 Returns the absolute value of arg. Arg may be either integer or
79 floating-point, and the result is returned in the same form.
80
81 acos arg
82 Returns the arc cosine of arg, in the range [0,pi] radians. Arg
83 should be in the range [-1,1].
84
85 asin arg
86 Returns the arc sine of arg, in the range [-pi/2,pi/2] radians.
87 Arg should be in the range [-1,1].
88
89 atan arg
90 Returns the arc tangent of arg, in the range [-pi/2,pi/2] radi‐
91 ans.
92
93 atan2 y x
94 Returns the arc tangent of y/x, in the range [-pi,pi] radians.
95 x and y cannot both be 0. If x is greater than 0, this is
96 equivalent to “atan [expr {y/x}]”.
97
98 bool arg
99 Accepts any numeric value, or any string acceptable to string is
100 boolean, and returns the corresponding boolean value 0 or 1.
101 Non-zero numbers are true. Other numbers are false. Non-
102 numeric strings produce boolean value in agreement with string
103 is true and string is false.
104
105 ceil arg
106 Returns the smallest integral floating-point value (i.e. with a
107 zero fractional part) not less than arg. The argument may be
108 any numeric value.
109
110 cos arg
111 Returns the cosine of arg, measured in radians.
112
113 cosh arg
114 Returns the hyperbolic cosine of arg. If the result would cause
115 an overflow, an error is returned.
116
117 double arg
118 The argument may be any numeric value, If arg is a floating-
119 point value, returns arg, otherwise converts arg to floating-
120 point and returns the converted value. May return Inf or -Inf
121 when the argument is a numeric value that exceeds the floating-
122 point range.
123
124 entier arg
125 The argument may be any numeric value. The integer part of arg
126 is determined and returned. The integer range returned by this
127 function is unlimited, unlike int and wide which truncate their
128 range to fit in particular storage widths.
129
130 exp arg
131 Returns the exponential of arg, defined as e**arg. If the
132 result would cause an overflow, an error is returned.
133
134 floor arg
135 Returns the largest integral floating-point value (i.e. with a
136 zero fractional part) not greater than arg. The argument may be
137 any numeric value.
138
139 fmod x y
140 Returns the floating-point remainder of the division of x by y.
141 If y is 0, an error is returned.
142
143 hypot x y
144 Computes the length of the hypotenuse of a right-angled trian‐
145 gle, approximately “sqrt [expr {x*x+y*y}]” except for being more
146 numerically stable when the two arguments have substantially
147 different magnitudes.
148
149 int arg
150 The argument may be any numeric value. The integer part of arg
151 is determined, and then the low order bits of that integer value
152 up to the machine word size are returned as an integer value.
153 For reference, the number of bytes in the machine word are
154 stored in the wordSize element of the tcl_platform array.
155
156 isqrt arg
157 Computes the integer part of the square root of arg. Arg must
158 be a positive value, either an integer or a floating point num‐
159 ber. Unlike sqrt, which is limited to the precision of a float‐
160 ing point number, isqrt will return a result of arbitrary preci‐
161 sion.
162
163 log arg
164 Returns the natural logarithm of arg. Arg must be a positive
165 value.
166
167 log10 arg
168 Returns the base 10 logarithm of arg. Arg must be a positive
169 value.
170
171 max arg ...
172 Accepts one or more numeric arguments. Returns the one argument
173 with the greatest value.
174
175 min arg ...
176 Accepts one or more numeric arguments. Returns the one argument
177 with the least value.
178
179 pow x y
180 Computes the value of x raised to the power y. If x is nega‐
181 tive, y must be an integer value.
182
183 rand Returns a pseudo-random floating-point value in the range (0,1).
184 The generator algorithm is a simple linear congruential genera‐
185 tor that is not cryptographically secure. Each result from rand
186 completely determines all future results from subsequent calls
187 to rand, so rand should not be used to generate a sequence of
188 secrets, such as one-time passwords. The seed of the generator
189 is initialized from the internal clock of the machine or may be
190 set with the srand function.
191
192 round arg
193 If arg is an integer value, returns arg, otherwise converts arg
194 to integer by rounding and returns the converted value.
195
196 sin arg
197 Returns the sine of arg, measured in radians.
198
199 sinh arg
200 Returns the hyperbolic sine of arg. If the result would cause
201 an overflow, an error is returned.
202
203 sqrt arg
204 The argument may be any non-negative numeric value. Returns a
205 floating-point value that is the square root of arg. May return
206 Inf when the argument is a numeric value that exceeds the square
207 of the maximum value of the floating-point range.
208
209 srand arg
210 The arg, which must be an integer, is used to reset the seed for
211 the random number generator of rand. Returns the first random
212 number (see rand) from that seed. Each interpreter has its own
213 seed.
214
215 tan arg
216 Returns the tangent of arg, measured in radians.
217
218 tanh arg
219 Returns the hyperbolic tangent of arg.
220
221 wide arg
222 The argument may be any numeric value. The integer part of arg
223 is determined, and then the low order 64 bits of that integer
224 value are returned as an integer value.
225
227 expr(n), mathop(n), namespace(n)
228
230 Copyright (c) 1993 The Regents of the University of California.
231 Copyright (c) 1994-2000 Sun Microsystems Incorporated.
232 Copyright (c) 2005, 2006 by Kevin B. Kenny <kennykb@acm.org>.
233
234
235
236Tcl 8.5 mathfunc(n)