1math::decimal(n) Tcl Decimal Arithmetic Library math::decimal(n)
2
3
4
5______________________________________________________________________________
6
8 math::decimal - General decimal arithmetic
9
11 package require Tcl ?8.5?
12
13 package require math::decimal 1.0.3
14
15 ::math::decimal::fromstr string
16
17 ::math::decimal::tostr decimal
18
19 ::math::decimal::setVariable variable setting
20
21 ::math::decimal::add a b
22
23 ::math::decimal::+ a b
24
25 ::math::decimal::subtract a b
26
27 ::math::decimal::- a b
28
29 ::math::decimal::multiply a b
30
31 ::math::decimal::* a b
32
33 ::math::decimal::divide a b
34
35 ::math::decimal::/ a b
36
37 ::math::decimal::divideint a b
38
39 ::math::decimal::remainder a b
40
41 ::math::decimal::abs decimal
42
43 ::math::decimal::compare a b
44
45 ::math::decimal::max a b
46
47 ::math::decimal::maxmag a b
48
49 ::math::decimal::min a b
50
51 ::math::decimal::minmag a b
52
53 ::math::decimal::plus a
54
55 ::math::decimal::minus a
56
57 ::math::decimal::copynegate a
58
59 ::math::decimal::copysign a b
60
61 ::math::decimal::is-signed decimal
62
63 ::math::decimal::is-zero decimal
64
65 ::math::decimal::is-NaN decimal
66
67 ::math::decimal::is-infinite decimal
68
69 ::math::decimal::is-finite decimal
70
71 ::math::decimal::fma a b c
72
73 ::math::decimal::round_half_even decimal digits
74
75 ::math::decimal::round_half_up decimal digits
76
77 ::math::decimal::round_half_down decimal digits
78
79 ::math::decimal::round_down decimal digits
80
81 ::math::decimal::round_up decimal digits
82
83 ::math::decimal::round_floor decimal digits
84
85 ::math::decimal::round_ceiling decimal digits
86
87 ::math::decimal::round_05up decimal digits
88
89______________________________________________________________________________
90
92 The decimal package provides decimal arithmetic support for both lim‐
93 ited precision floating point and arbitrary precision floating point.
94 Additionally, integer arithmetic is supported.
95
96 More information and the specifications on which this package depends
97 can be found on the general decimal arithmetic page at
98 http://speleotrove.com/decimal This package provides for:
99
100 · A new data type decimal which is represented as a list contain‐
101 ing sign, mantissa and exponent.
102
103 · Arithmetic operations on those decimal numbers such as addition,
104 subtraction, multiplication, etc...
105
106 Numbers are converted to decimal format using the operation
107 ::math::decimal::fromstr.
108
109 Numbers are converted back to string format using the operation
110 ::math::decimal::tostr.
111
113 This section shows some simple examples. Since the purpose of this
114 library is to perform decimal math operations, examples may be the sim‐
115 plest way to learn how to work with it and to see the difference
116 between using this package and sticking with expr. Consult the API sec‐
117 tion of this man page for information about individual procedures.
118
119 package require math::decimal
120
121 # Various operations on two numbers.
122 # We first convert them to decimal format.
123 set a [::math::decimal::fromstr 8.2]
124 set b [::math::decimal::fromstr .2]
125
126 # Then we perform our operations. Here we add
127 set c [::math::decimal::+ $a $b]
128
129 # Finally we convert back to string format for presentation to the user.
130 puts [::math::decimal::tostr $c] ; # => will output 8.4
131
132 # Other examples
133 #
134 # Subtraction
135 set c [::math::decimal::- $a $b]
136 puts [::math::decimal::tostr $c] ; # => will output 8.0
137
138 # Why bother using this instead of simply expr?
139 puts 8.399999999999999 ; # => will output 8.399999999999999
140 puts 7.999999999999999 ; # => will output 7.999999999999999
141 # See http://speleotrove.com/decimal to learn more about why this happens.
142
143
145 ::math::decimal::fromstr string
146 Convert string into a decimal.
147
148 ::math::decimal::tostr decimal
149 Convert decimal into a string representing the number in base
150 10.
151
152 ::math::decimal::setVariable variable setting
153 Sets the variable to setting. Valid variables are:
154
155 · rounding - Method of rounding to use during rescale.
156 Valid methods are round_half_even, round_half_up,
157 round_half_down, round_down, round_up, round_floor,
158 round_ceiling.
159
160 · precision - Maximum number of digits allowed in mantissa.
161
162 · extended - Set to 1 for extended mode. 0 for simplified
163 mode.
164
165 · maxExponent - Maximum value for the exponent. Defaults to
166 999.
167
168 · minExponent - Minimum value for the exponent. Default to
169 -998.
170
171 ::math::decimal::add a b
172
173 ::math::decimal::+ a b
174 Return the sum of the two decimals a and b.
175
176 ::math::decimal::subtract a b
177
178 ::math::decimal::- a b
179 Return the differnece of the two decimals a and b.
180
181 ::math::decimal::multiply a b
182
183 ::math::decimal::* a b
184 Return the product of the two decimals a and b.
185
186 ::math::decimal::divide a b
187
188 ::math::decimal::/ a b
189 Return the quotient of the division between the two decimals a
190 and b.
191
192 ::math::decimal::divideint a b
193 Return a the integer portion of the quotient of the division
194 between decimals a and b
195
196 ::math::decimal::remainder a b
197 Return the remainder of the division between the two decimals a
198 and b.
199
200 ::math::decimal::abs decimal
201 Return the absolute value of the decimal.
202
203 ::math::decimal::compare a b
204 Compare the two decimals a and b, returning 0 if a == b, 1 if a
205 > b, and -1 if a < b.
206
207 ::math::decimal::max a b
208 Compare the two decimals a and b, and return a if a >= b, and b
209 if a < b.
210
211 ::math::decimal::maxmag a b
212 Compare the two decimals a and b while ignoring their signs, and
213 return a if abs(a) >= abs(b), and b if abs(a) < abs(b).
214
215 ::math::decimal::min a b
216 Compare the two decimals a and b, and return a if a <= b, and b
217 if a > b.
218
219 ::math::decimal::minmag a b
220 Compare the two decimals a and b while ignoring their signs, and
221 return a if abs(a) <= abs(b), and b if abs(a) > abs(b).
222
223 ::math::decimal::plus a
224 Return the result from ::math::decimal::+ 0 $a.
225
226 ::math::decimal::minus a
227 Return the result from ::math::decimal::- 0 $a.
228
229 ::math::decimal::copynegate a
230 Returns a with the sign flipped.
231
232 ::math::decimal::copysign a b
233 Returns a with the sign set to the sign of the b.
234
235 ::math::decimal::is-signed decimal
236 Return the sign of the decimal. The procedure returns 0 if the
237 number is positive, 1 if it's negative.
238
239 ::math::decimal::is-zero decimal
240 Return true if decimal value is zero, otherwise false is
241 returned.
242
243 ::math::decimal::is-NaN decimal
244 Return true if decimal value is NaN (not a number), otherwise
245 false is returned.
246
247 ::math::decimal::is-infinite decimal
248 Return true if decimal value is Infinite, otherwise false is
249 returned.
250
251 ::math::decimal::is-finite decimal
252 Return true if decimal value is finite, otherwise false is
253 returned.
254
255 ::math::decimal::fma a b c
256 Return the result from first multiplying a by b and then adding
257 c. Rescaling only occurs after completion of all operations. In
258 this way the result may vary from that returned by performing
259 the operations individually.
260
261 ::math::decimal::round_half_even decimal digits
262 Rounds decimal to digits number of decimal points with the fol‐
263 lowing rules: Round to the nearest. If equidistant, round so the
264 final digit is even.
265
266 ::math::decimal::round_half_up decimal digits
267 Rounds decimal to digits number of decimal points with the fol‐
268 lowing rules: Round to the nearest. If equidistant, round up.
269
270 ::math::decimal::round_half_down decimal digits
271 Rounds decimal to digits number of decimal points with the fol‐
272 lowing rules: Round to the nearest. If equidistant, round down.
273
274 ::math::decimal::round_down decimal digits
275 Rounds decimal to digits number of decimal points with the fol‐
276 lowing rules: Round toward 0. (Truncate)
277
278 ::math::decimal::round_up decimal digits
279 Rounds decimal to digits number of decimal points with the fol‐
280 lowing rules: Round away from 0
281
282 ::math::decimal::round_floor decimal digits
283 Rounds decimal to digits number of decimal points with the fol‐
284 lowing rules: Round toward -Infinity.
285
286 ::math::decimal::round_ceiling decimal digits
287 Rounds decimal to digits number of decimal points with the fol‐
288 lowing rules: Round toward Infinity
289
290 ::math::decimal::round_05up decimal digits
291 Rounds decimal to digits number of decimal points with the fol‐
292 lowing rules: Round zero or five away from 0. The same as round-
293 up, except that rounding up only occurs if the digit to be
294 rounded up is 0 or 5, and after overflow the result is the same
295 as for round-down.
296
298 This document, and the package it describes, will undoubtedly contain
299 bugs and other problems. Please report such in the category decimal of
300 the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please
301 also report any ideas for enhancements you may have for either package
302 and/or documentation.
303
304 When proposing code changes, please provide unified diffs, i.e the out‐
305 put of diff -u.
306
307 Note further that attachments are strongly preferred over inlined
308 patches. Attachments can be made by going to the Edit form of the
309 ticket immediately after its creation, and then using the left-most
310 button in the secondary navigation bar.
311
313 decimal, math, tcl
314
316 Mathematics
317
319 Copyright (c) 2011 Mark Alston <mark at beernut dot com>
320
321
322
323
324tcllib 1.0.3 math::decimal(n)