1math::exact(n)                 Tcl Math Library                 math::exact(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       math::exact - Exact Real Arithmetic
9

SYNOPSIS

11       package require Tcl  8.6
12
13       package require grammar::aycock  1.0
14
15       package require math::exact  1.0.1
16
17       ::math::exact::exactexpr expr
18
19       number ref
20
21       number unref
22
23       number asPrint precision
24
25       number asFloat precision
26
27______________________________________________________________________________
28

DESCRIPTION

30       The  exactexpr command in the math::exact package allows for exact com‐
31       putations over the computable real numbers.  These are  not  arbitrary-
32       precision calculations; rather they are exact, with numbers represented
33       by algorithms that produce successive approximations. At the end  of  a
34       calculation,  the  caller  can  request  a  given precision for the end
35       result, and intermediate results are computed to whatever precision  is
36       necessary to satisfy the request.
37

PROCEDURES

39       The following procedure is the primary entry into the math::exact pack‐
40       age.
41
42       ::math::exact::exactexpr expr
43              Accepts a mathematical expression in Tcl syntax, and returns  an
44              object  that  represents  the  program  to  calculate successive
45              approximations to the expression's value.  The  result  will  be
46              referred to as an exact real number.
47
48       number ref
49              Increases the reference count of a given exact real number.
50
51       number unref
52              Decreases  the reference count of a given exact real number, and
53              destroys the number if the reference count is zero.
54
55       number asPrint precision
56              Formats the given number for printing, with the specified preci‐
57              sion.   (See  below  for  how precision is interpreted). Numbers
58              that are known to be rational are formatted as fractions.
59
60       number asFloat precision
61              Formats the given number for printing, with the specified preci‐
62              sion.  (See below for how precision is interpreted). All numbers
63              are formatted in floating-point E format.
64

PARAMETERS

66       expr   Expression to evaluate. The syntax for expressions is  the  same
67              as  it  is  in  Tcl,  but  the set of operations is smaller. See
68              Expressions below for details.
69
70       number The   object   returned   by   an    earlier    invocation    of
71              math::exact::exactexpr
72
73       precision
74              The  requested  'precision'  of  the  result.  The  precision is
75              (approximately) the absolute value of the binary  exponent  plus
76              the  number  of bits of the binary significand. For instance, to
77              return results to IEEE-754 double precision, 56  bits  plus  the
78              exponent  are required. Numbers between 1/2 and 2 will require a
79              precision of 57; numbers between 1/4 and 1/2 or between 2 and  4
80              will  require 58; numbers between 1/8 and 1/4 or between 4 and 8
81              will require 59; and so on.
82

EXPRESSIONS

84       The math::exact::exactexpr command accepts expressions in a  subset  of
85       Tcl's syntax. The following components may be used in an expression.
86
87       ·      Decimal integers.
88
89       ·      Variable  references with the dollar sign ($).  The value of the
90              variable   must   be   the   result   of   another    call    to
91              math::exact::exactexpr. The reference count of the value will be
92              increased by one for each position at which it  appears  in  the
93              expression.
94
95       ·      The exponentiation operator (**).
96
97       ·      Unary plus (+) and minus (-) operators.
98
99       ·      Multiplication (*) and division (/) operators.
100
101       ·      Parentheses used for grouping.
102
103       ·      Functions. See Functions below for the functions that are avail‐
104              able.
105

FUNCTIONS

107       The following functions are available for use within exact real expres‐
108       sions.
109
110       acos(x)
111              The  inverse  cosine  of  x. The result is expressed in radians.
112              The absolute value of x must be less than 1.
113
114       acosh(x)
115              The inverse hyperbolic cosine of x.  x must be greater than 1.
116
117       asin(x)
118              The inverse sine of x. The result is expressed in radians.   The
119              absolute value of x must be less than 1.
120
121       asinh(x)
122              The inverse hyperbolic sine of x.
123
124       atan(x)
125              The inverse tangent of x. The result is expressed in radians.
126
127       atanh(x)
128              The  inverse  hyperbolic  tangent of x.  The absolute value of x
129              must be less than 1.
130
131       cos(x) The cosine of x. x is expressed in radians.
132
133       cosh(x)
134              The hyperbolic cosine of x.
135
136       e()    The base of the natural logarithms = 2.71828...
137
138       exp(x) The exponential function of x.
139
140       log(x) The natural logarithm of x. x must be positive.
141
142       pi()   The value of pi = 3.15159...
143
144       sin(x) The sine of x. x is expressed in radians.
145
146       sinh(x)
147              The hyperbolic sine of x.
148
149       sqrt(x)
150              The square root of x. x must be positive.
151
152       tan(x) The tangent of x. x is expressed in radians.
153
154       tanh(x)
155              The hyperbolic tangent of x.
156

SUMMARY

158       The math::exact::exactexpr command  provides  a  system  that  performs
159       exact arithmetic over computable real numbers, representing the numbers
160       as algorithms for successive approximation.  An example,  which  imple‐
161       ments the high-school quadratic formula, is shown below.
162
163
164              namespace import math::exact::exactexpr
165              proc exactquad {a b c} {
166                  set d [[exactexpr {sqrt($b*$b - 4*$a*$c)}] ref]
167                  set r0 [[exactexpr {(-$b - $d) / (2 * $a)}] ref]
168                  set r1 [[exactexpr {(-$b + $d) / (2 * $a)}] ref]
169                  $d unref
170                  return [list $r0 $r1]
171              }
172
173              set a [[exactexpr 1] ref]
174              set b [[exactexpr 200] ref]
175              set c [[exactexpr {(-3/2) * 10**-12}] ref]
176              lassign [exactquad $a $b $c] r0 r1
177              $a unref; $b unref; $c unref
178              puts [list [$r0 asFloat 70] [$r1 asFloat 110]]
179              $r0 unref; $r1 unref
180
181       The program prints the result:
182
183
184              -2.000000000000000075e2 7.499999999999999719e-15
185
186       Note  that  if  IEEE-754  floating  point had been used, a catastrophic
187       roundoff error would yield a smaller root that is a factor of  two  too
188       high:
189
190
191              -200.0 1.4210854715202004e-14
192
193       The  invocations  of  exactexpr should be fairly self-explanatory.  The
194       other commands of note are ref and unref. It is necessary for the call‐
195       er to keep track of references to exact expressions - to call ref every
196       time an exact expression is stored in a variable and unref  every  time
197       the  variable  goes out of scope or is overwritten.  The asFloat method
198       emits decimal digits as long as the requested precision supports  them.
199       It  terminates  when  the  requested precision yields an uncertainty of
200       more than one unit in the least significant digit.
201

CATEGORY

203       Mathematics
204
206       Copyright (c) 2015 Kevin B. Kenny <kennykb@acm.org>
207       Redistribution permitted under the terms of the Open Publication License <http://www.opencontent.org/openpub/>
208
209
210
211
212tcllib                               1.0.1                      math::exact(n)
Impressum