SlideShare a Scribd company logo
1 of 38
Exact Real Arithmetic in Tcl
Kevin B. Kenny
22nd
Annual Tcl/Tk Conference
21 October 2015
Have you ever hadHave you ever had
a software problema software problem
caused bycaused by
floating point roundoff?floating point roundoff?
Were you using floating pointWere you using floating point
for currency?for currency?
Did you learn from that mistake?
You havenYou haven’’t had floating-pointt had floating-point
precision problems?precision problems?
How do you know?How do you know?
Do you test your softwareDo you test your software
for the accuracy offor the accuracy of
floating-point results?floating-point results?
How do you know whatHow do you know what
the right answers are?the right answers are?
Floating point is full of horror storiesFloating point is full of horror stories
Many involve the precisionMany involve the precision
of intermediate results,of intermediate results,
not the input data nornot the input data nor
the ultimate answersthe ultimate answers
Catastrophic loss of significanceCatastrophic loss of significance
LetLet’’s say that we have the equations say that we have the equation
A x
2
+B x+C=0
And we want to findAnd we want to find xx. Asking the nearest high-. Asking the nearest high-
schooler for how to do it we get told,schooler for how to do it we get told,
x=
−B±√B
2
−4 A C
2 A
Everyone knows that! Brahmagupta published it inEveryone knows that! Brahmagupta published it in
A.D. 678! People have used that formula for 1500A.D. 678! People have used that formula for 1500
years!years!
Catastrophic loss of significanceCatastrophic loss of significance
proc quad1 {a b c} {proc quad1 {a b c} {
set d [expr {sqrt($b*$b – 4.*$a*$c)}]set d [expr {sqrt($b*$b – 4.*$a*$c)}]
set r0 [expr {(-$b - $d) / (2. * $a)}]set r0 [expr {(-$b - $d) / (2. * $a)}]
set r1 [expr {(-$b + $d) / (2. * $a)}]set r1 [expr {(-$b + $d) / (2. * $a)}]
return [list $r0 $r1]return [list $r0 $r1]
}}
quad1 1.0 200.0 -1.5e-12quad1 1.0 200.0 -1.5e-12
→→ -200.0-200.0 1.4210854715202004e-141.4210854715202004e-14
What?What?
The correct answer is approximately 7.5The correct answer is approximately 7.5××1010-15-15
!!
(The answer:(The answer: -200. - sqrt(400.000000000006)-200. - sqrt(400.000000000006) losesloses
all but one bit of the significand.)all but one bit of the significand.)
YouYou’’re doing it wrong!re doing it wrong!
An experienced numerical analyst will tell you thatAn experienced numerical analyst will tell you that
the right way to use the quadratic formula is tothe right way to use the quadratic formula is to
write it in a different way:write it in a different way:
x=
−B±√B2
−4 A C
2 A
x=
2C
−B±√B
2
−4 A C
Experienced numerical analysts are expensive.Experienced numerical analysts are expensive.
They can take a long time to solve your problem.They can take a long time to solve your problem.
They still make mistakes and miss things.They still make mistakes and miss things.
How do you know whenHow do you know when theythey have it right?have it right?
A popular idea: variable precisionA popular idea: variable precision
Repeat calculations at different precisionsRepeat calculations at different precisions
See what changes as precision increases: do weSee what changes as precision increases: do we
get the same answer?get the same answer?
But consider the problem of finding the limit of:But consider the problem of finding the limit of:
x0 = 4.00
x1 = 4.25
xn = 108−
815−
1500
xn−2
xn−1
Variable precision: what happens?Variable precision: what happens?
# │ float │ double │ exact# │ float │ double │ exact
───┼───────────┼───────────┼──────────────┼───────────┼───────────┼───────────
1 │ 4.47059 │ 4.47059 │ 4.470591 │ 4.47059 │ 4.47059 │ 4.47059
2 │ 4.64474 │ 4.64474 │ 4.644742 │ 4.64474 │ 4.64474 │ 4.64474
3 │ 4.77053 │ 4.77054 │ 4.770543 │ 4.77053 │ 4.77054 │ 4.77054
4 │ 4.85545 │ 4.85570 │ 4.855704 │ 4.85545 │ 4.85570 │ 4.85570
5 │ 4.90575 │ 4.91085 │ 4.910855 │ 4.90575 │ 4.91085 │ 4.91085
6 │ 4.84165 │ 4.94554 │ 4.945546 │ 4.84165 │ 4.94554 │ 4.94554
7 │ 2.82180 │ 4.96696 │ 4.966967 │ 2.82180 │ 4.96696 │ 4.96696
8 │ -71.03029 │ 4.98004 │ 4.980048 │ -71.03029 │ 4.98004 │ 4.98004
9 │ 111.99020 │ 4.98791 │ 4.987989 │ 111.99020 │ 4.98791 │ 4.98798
10 │ 100.53401 │ 4.99136 │ 4.9927710 │ 100.53401 │ 4.99136 │ 4.99277
11 │ 100.02652 │ 4.96746 │ 4.9956611 │ 100.02652 │ 4.96746 │ 4.99566
12 │ 100.00133 │ 4.42969 │ 4.9973912 │ 100.00133 │ 4.42969 │ 4.99739
13 │ 100.00007 │ -7.81724 │ 4.9984313 │ 100.00007 │ -7.81724 │ 4.99843
14 │ 100.00000 │ 168.93917 │ 4.9990614 │ 100.00000 │ 168.93917 │ 4.99906
15 │ 100.00000 │ 102.03996 │ 4.9994415 │ 100.00000 │ 102.03996 │ 4.99944
16 │ 100.00000 │ 100.09995 │ 4.9996616 │ 100.00000 │ 100.09995 │ 4.99966
17 │ 100.00000 │ 100.00499 │ 4.9998017 │ 100.00000 │ 100.00499 │ 4.99980
18 │ 100.00000 │ 100.00025 │ 4.9998818 │ 100.00000 │ 100.00025 │ 4.99988
IEEE-754 ‘float’gives
1decimalplace,
thenexplodes!
IEEE-754 ‘double’manages2
decimalplaces,thenexplodes
intheexactsameway!
Essentially all floating-
point systems
converge on 100. It's
easy to overlook the
wrong answer.
What if we hadWhat if we had exactexact arithmetic?arithmetic?
●
Control the precision of the outputControl the precision of the output
●
Compute intermediate results to whatever levelCompute intermediate results to whatever level
of precision is neededof precision is needed
● ReplaceReplace ‘‘numbernumber’’ withwith ‘algorithmtocomputethe‘algorithmtocomputethe numbernumber’’
Exact arithmetic packageExact arithmetic package
TcllibTcllib math::exactmath::exact package. (Pure Tcl.)package. (Pure Tcl.)
Numbers (the result of calculations) are TclOO objects.Numbers (the result of calculations) are TclOO objects.
Reference counted objects – which is unpleasant.Reference counted objects – which is unpleasant.
Created byCreated by math::exact::exactexprmath::exact::exactexpr
Can format themselves withCan format themselves with asPrintasPrint andand asFloatasFloat
methods.methods.
Methods take desired precision. (Precision is the sum ofMethods take desired precision. (Precision is the sum of
the number of bits of exponent plus the number of bits ofthe number of bits of exponent plus the number of bits of
significand.)significand.)
Exact arithmetic - exampleExact arithmetic - example
%% package require math::exactpackage require math::exact
1.01.0
%% namespace import math::exact::exactexprnamespace import math::exact::exactexpr
%% set r [[exactexpr {exp(pi()*sqrt(163))}] ref]set r [[exactexpr {exp(pi()*sqrt(163))}] ref]
::oo::Obj118::oo::Obj118
%% $r asFloat 108$r asFloat 108
2.62537412640768e172.62537412640768e17
%% $r asFloat 200$r asFloat 200
2.625374126407687439999999999992500725971981e172.625374126407687439999999999992500725971981e17
%% set f [[exactexpr {$r-262537412640768744}] ref]set f [[exactexpr {$r-262537412640768744}] ref]
::oo::Obj125::oo::Obj125
%% $f asFloat 100$f asFloat 100
-7.49927402801814311e-13-7.49927402801814311e-13
%% $f unref$f unref
%% $r unref$r unref
Exact arithmetic:Exact arithmetic:
fixing the quadratic formulafixing the quadratic formula
proc exactquad {a b c} {proc exactquad {a b c} {
set d [[exactexpr {sqrt($b*$b - 4*$a*$c)}] ref]set d [[exactexpr {sqrt($b*$b - 4*$a*$c)}] ref]
set r0 [[exactexpr {(-$b - $d)set r0 [[exactexpr {(-$b - $d)
/ (2 * $a)}] ref]/ (2 * $a)}] ref]
set r1 [[exactexpr {(-$b + $d)set r1 [[exactexpr {(-$b + $d)
/ (2 * $a)}] ref]/ (2 * $a)}] ref]
$d unref$d unref
return [list $r0 $r1]return [list $r0 $r1]
}}
Exact arithmetic:Exact arithmetic:
fixing the quadratic formulafixing the quadratic formula
set a [[exactexpr 1] ref]set a [[exactexpr 1] ref]
set b [[exactexpr 200] ref]set b [[exactexpr 200] ref]
set c [[exactexpr {(-3/2) * 10**-12}] ref]set c [[exactexpr {(-3/2) * 10**-12}] ref]
lassign [exactquad $a $b $c] r0 r1lassign [exactquad $a $b $c] r0 r1
$a unref; $b unref; $c unref$a unref; $b unref; $c unref
puts [list [$r0 asFloat 70] [$r1 asFloat 110]]puts [list [$r0 asFloat 70] [$r1 asFloat 110]]
$r0 unref; $r1 unref$r0 unref; $r1 unref
-2.000000000000000075e2 7.499999999999999719e-15-2.000000000000000075e2 7.499999999999999719e-15
No manipulation of the formulas! Just ask for 70 “bits” forNo manipulation of the formulas! Just ask for 70 “bits” for
the larger root and 110 “bits” for the smaller, and you getthe larger root and 110 “bits” for the smaller, and you get
them.them.
Major disadvantagesMajor disadvantages
●
SLOW – but for generating constants inSLOW – but for generating constants in
advance (for use in algorithms or for testing),advance (for use in algorithms or for testing),
probably okprobably ok
●
No comparison operators (I'll explain…)No comparison operators (I'll explain…)
●
No floating point notation on inputNo floating point notation on input
●
Ref counting rather than automatic reclamationRef counting rather than automatic reclamation
No comparison?No comparison?
●
Comparison of reals isn't decidable.Comparison of reals isn't decidable.
●
What you can have are operatorsWhat you can have are operators
a<ϵ b,a>ϵ b,a=ϵ b,...
|b−a|≤ϵ
These comparisons are computible.
Need a notation for specifying these in
expressions.
which are allowed to return the wrong answer if
No floating point notation?No floating point notation?
What doesWhat does 0.33333333333333330.3333333333333333 mean?mean?
●
The fractionThe fraction
3333333333333333/10000000000000000, exactly3333333333333333/10000000000000000, exactly
representing the string representation?representing the string representation?
●
The fractionThe fraction
6004799503160661/18014398509481984,6004799503160661/18014398509481984,
exactly representing the internal representation?exactly representing the internal representation?
●
The fraction 1/3, which will have string and internalThe fraction 1/3, which will have string and internal
representations as above?representations as above?
For now, say what you mean! (Suggestions for what'sFor now, say what you mean! (Suggestions for what's
reasonable are welcome!)reasonable are welcome!)
No garbage collection?No garbage collection?
●
The fragile reference problemThe fragile reference problem
●
Common to all interfaces between Tcl andCommon to all interfaces between Tcl and
managed code systems (e.g., Java, .NET)managed code systems (e.g., Java, .NET)
●
Bigger topic than just exact realsBigger topic than just exact reals
●
Generic extension for adding fragile referencesGeneric extension for adding fragile references
exists. (Implemented originally for TCOM)exists. (Implemented originally for TCOM)
http://www.vex.net/~cthuang/counted/http://www.vex.net/~cthuang/counted/
How am I doing for time?
Want to explore under the hood?
(Warning, some mathematics ahead...)
Digit streamsDigit streams
Digit streams, at first look attractive.Digit streams, at first look attractive.
Make each algorithm into a coroutine that returnsMake each algorithm into a coroutine that returns
decimal or binary digits.decimal or binary digits.
But there is a problem: things get jammed up!But there is a problem: things get jammed up!
What is 1/6What is 1/6 ×× 9 in decimal?9 in decimal?
(It is 1.5, right?)(It is 1.5, right?)
Recall that 1/6 = 0.16666….Recall that 1/6 = 0.16666….
1/61/6 ×× 99
0.90.9 ≤≤ 0.1…0.1… ×× 99 1.8≤1.8≤
NotenoughinformationtooutputadigitNotenoughinformationtooutputadigit
1.441.44 ≤≤ 0.16…0.16… ×× 99 1.53≤1.53≤
Outputtheleadingdigit1Outputtheleadingdigit1
1.494≤1.494≤ 0.166…×0.166…× 99 1.503≤1.503≤
1.4994≤1.4994≤ 0.1666…×0.1666…× 99 1.5003≤1.5003≤
1.49994≤1.49994≤ 0.16666…×0.16666…× 99 1.50003≤1.50003≤
Ithinkwe'restuckinaloop.Ithinkwe'restuckinaloop.
What about continued fractions?What about continued fractions?
x=a+
1
b+
1
c+
1
⋱
Terminate for all the rationals.
Periodic for quadratic surds.
The same problem with getting
jammed – just more complicated
expressions. What is sqrt(2)**2?
Sqrt(2) ≤ 1+1/ 2(1.5) Sqrt(2)**2 ≤ 2.25
Sqrt(2) ≥ 1+1/(2+1) (1.333…) Sqrt(2)**2 ≥ 1.777...
Sqrt(2) ≥ 1+1/(2+1/2) (1.4) Sqrt(2)**2 ≥ 1.96
Sqrt(2) ≤ 1+1/(2+1/(2+1)) (1.42857…) Sqrt(2)**2 ≤ 2.04…
Sqrt(2) ≤ 1+1/(2+1/(2+1/2))) (1.41666…) Sqrt(2)**2 ≤ 2.0069444...
Sqrt(2) ≥ 1+1/(2+1/(2+1/(2+1)))) (1.41176…) Sqrt(2)**2 ≥ 1.993...
We can never decide whether the a term of the result is
1 or 2!
Why do these ideas fail?Why do these ideas fail?
●
If numbers are programs, exactIf numbers are programs, exact
comparison (equality, forcomparison (equality, for
example) is the Halting Problem.example) is the Halting Problem.
●
Alan Turing was originally anAlan Turing was originally an
analyst, not a logician.analyst, not a logician.
●
TuringTuring’’s work on the Haltings work on the Halting
Problem was a byproduct of hisProblem was a byproduct of his
attempt to put the real numbersattempt to put the real numbers
on a sound theoretical footing.on a sound theoretical footing.
Exact comparison of real numbersExact comparison of real numbers
is not decidable!is not decidable!
Use weak comparisonsUse weak comparisons
a<ϵ b,a>ϵ b,a=ϵ b,...
Allowed to return the wrong answer if
|b−a|≤ϵ
Weak comparisons are computible.
Use redundant representationsUse redundant representations
More than one way to write a given numberMore than one way to write a given number
Can choose at least one correct way to write aCan choose at least one correct way to write a
number using only weak comparisons.number using only weak comparisons.
Rational numbers and integerRational numbers and integer
vectorsvectors
Represent the rational number a/b by the vectorRepresent the rational number a/b by the vector
{a b}{a b}
(Assume reduction to lowest terms).(Assume reduction to lowest terms).
Division by zero is OK, and there is a single pointDivision by zero is OK, and there is a single point
at infinity.at infinity.
a
b
b=1
(a/b,1)
Möbius transformationsMöbius transformations
The matrixThe matrix
[a c
b d]
represents the function
a x+c
b x+d a
IfIf xx>0>0, this is also the, this is also the
generalized intervalgeneralized interval
[[cc//dd .... aa//bb], moving], moving
clockwise on the circleclockwise on the circle
a
b
(c/d ,1)
(a/b,1)
Composition of transformationsComposition of transformations
If f (x)=
a x+c
b x+d
, and g(x)=
e x+g
f x+h
,
then f (g(x))=
(ae+c f )x+(a g+ch)
(be+d f )x+(b g+dh)
This is just matrix multiplication:
[a c
b d][e g
f h]=[ae+c f a g+ch
be+d f bg+d h]
Relation to continued fractionsRelation to continued fractions
a+
1
b+
1
c+
1
⋱
can be written[a 1
1 0]⋅[b 1
1 0]⋅[c 1
1 0]⋅⋯.
Real numbers are streams ofReal numbers are streams of
matrices.matrices.
Bilinear transformationsBilinear transformations
We also use 2We also use 2××22××2 tensors. If2 tensors. If xx andand yy are vectors,are vectors,
write:write:
[a e
b f |c g
d h]L x R y , meaning
a x y+b x+c y+d
e x y+f x+g y+h
IfIf xx andand yy are vectors,are vectors, AA andand BB are matrices,are matrices, TT is ais a
tensor, all of the following are well defined:tensor, all of the following are well defined:
A⋅x vector T L x matrix T R y vector
A⋅B matrix T L A matrix T R B matrix
A⋅T tensor
Arithmetic on exact realsArithmetic on exact reals
Remembering thatRemembering that
[a e
b f |c g
d h]L x R y means
a x y+b x+c y+d
e x y+f x+g y+h
The following special tensors will be useful.The following special tensors will be useful.
[0 1
0 0|1 0
0 1] addition [1 0
0 0|0 0
0 1] multiplication
[0 1
0 0|−1 0
0 1] subtraction [0 1
0 0|0 0
1 0] division
Expressions are trees of tensors,Expressions are trees of tensors,
matrices, vectorsmatrices, vectors
●
Special functions and non-integer powers areSpecial functions and non-integer powers are
infinite treesinfinite trees
●
Lazy evaluation. A node in a special functionLazy evaluation. A node in a special function
knows how to instantiate its children and doesknows how to instantiate its children and does
so only when it needs to.so only when it needs to.
●
Example:Example:
√x=[x+1 2 x
2 x+1]⋅[x+1 2 x
2 x+1]⋅[x+1 2x
2 x+1]⋅⋯
ln x=[x−1 x−1
1 x ]⋅∏
n=1
∞
[n x+n+1 (2n+1)x
2n+1 (n+1)x+n]
Sign matricesSign matrices
A sign matrix selects half the number circle.A sign matrix selects half the number circle.
S−=[0 −1
1 0 ] S∞=[ 1 1
−1 1]S+=[1 0
0 1] S0=[1 −1
1 1 ]
Every number's stream begins with a sign matrix.Every number's stream begins with a sign matrix.
Always have two choices – decidable.Always have two choices – decidable.
Remaining matrices in the stream will have all elementsRemaining matrices in the stream will have all elements
nonnegative.nonnegative.
Digit matricesDigit matrices
●
Digit matrices slice up the nonnegative half-lineDigit matrices slice up the nonnegative half-line
D−=[1 0
1 2] D+=[2 1
0 1]D0=[3 1
1 3]
0 1/3 1 3
Each positive number is aEach positive number is a
stream of digit matrices.stream of digit matrices.
Each digit matrix conveysEach digit matrix conveys
≈≈1 bit of information.1 bit of information.
Pulling the streamPulling the stream
●
For each possible digit matrixFor each possible digit matrix DD, a matrix, a matrix MM or tensoror tensor TT
asks whether it can prove thatasks whether it can prove that DD−1−1
xx is still positive.is still positive.
●
If so, it emitsIf so, it emits DD and replaces itself withand replaces itself with DD−1−1
xx..
●
If no digit is emitted, then a matrixIf no digit is emitted, then a matrix MM asks for a digitasks for a digit EE
from its argument, multiplies itself by the digit and triesfrom its argument, multiplies itself by the digit and tries
again.again.
●
A tensorA tensor TT will ask its left and right arguments inwill ask its left and right arguments in
succession and replace itself withsuccession and replace itself with
TT LL EE oror TT RR EE respectivelyrespectively
●
Every digit can be delivered in a finite number of stepsEvery digit can be delivered in a finite number of steps

More Related Content

What's hot

Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perltinypigdotcom
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick referenceArduino Aficionado
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Contextlichtkind
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad Shaker
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Logging considered-harmful
Logging considered-harmfulLogging considered-harmful
Logging considered-harmfuldd105
 
Php in the graph (Gremlin 3)
Php in the graph (Gremlin 3)Php in the graph (Gremlin 3)
Php in the graph (Gremlin 3)Damien Seguy
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Kevlin Henney
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smartlichtkind
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }John De Goes
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer. Haim Michael
 

What's hot (20)

Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Slides
SlidesSlides
Slides
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
C++ L06-Pointers
C++ L06-PointersC++ L06-Pointers
C++ L06-Pointers
 
Logging considered-harmful
Logging considered-harmfulLogging considered-harmful
Logging considered-harmful
 
Php in the graph (Gremlin 3)
Php in the graph (Gremlin 3)Php in the graph (Gremlin 3)
Php in the graph (Gremlin 3)
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
Haskell Jumpstart
Haskell JumpstartHaskell Jumpstart
Haskell Jumpstart
 
Get Kata
Get KataGet Kata
Get Kata
 
Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Trading on SPY
Trading on SPYTrading on SPY
Trading on SPY
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 

Similar to Exact Real Arithmetic for Tcl

C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inTIB Academy
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with RYanchang Zhao
 
An introduction to probabilistic data structures
An introduction to probabilistic data structuresAn introduction to probabilistic data structures
An introduction to probabilistic data structuresMiguel Ping
 
Yapc Asia 2008 TMTOWTMS
Yapc Asia 2008 TMTOWTMSYapc Asia 2008 TMTOWTMS
Yapc Asia 2008 TMTOWTMSJeen Lee
 
Deep Learning and Design Thinking
Deep Learning and Design ThinkingDeep Learning and Design Thinking
Deep Learning and Design ThinkingYen-lung Tsai
 
@RISK Unchained Webinar
@RISK Unchained Webinar@RISK Unchained Webinar
@RISK Unchained WebinarAndrew Sich
 
Assumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfAssumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfErin Shellman
 
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算Xue Xin Tsai
 
State Space Search
State Space SearchState Space Search
State Space SearchJasmine Chen
 
OSMC 2013 | Making monitoring simple? by Michael Medin
OSMC 2013 | Making monitoring simple? by Michael MedinOSMC 2013 | Making monitoring simple? by Michael Medin
OSMC 2013 | Making monitoring simple? by Michael MedinNETWAYS
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listingChris Worledge
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comBaileya126
 
CIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.comCIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.comclaric130
 
Cis 115 Effective Communication / snaptutorial.com
Cis 115  Effective Communication / snaptutorial.comCis 115  Effective Communication / snaptutorial.com
Cis 115 Effective Communication / snaptutorial.comBaileyao
 

Similar to Exact Real Arithmetic for Tcl (20)

C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
 
Microchip Mfg. problem
Microchip Mfg. problemMicrochip Mfg. problem
Microchip Mfg. problem
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with R
 
An introduction to probabilistic data structures
An introduction to probabilistic data structuresAn introduction to probabilistic data structures
An introduction to probabilistic data structures
 
Test Automation Day 2018
Test Automation Day 2018Test Automation Day 2018
Test Automation Day 2018
 
Yapc Asia 2008 TMTOWTMS
Yapc Asia 2008 TMTOWTMSYapc Asia 2008 TMTOWTMS
Yapc Asia 2008 TMTOWTMS
 
Deep Learning and Design Thinking
Deep Learning and Design ThinkingDeep Learning and Design Thinking
Deep Learning and Design Thinking
 
@RISK Unchained Webinar
@RISK Unchained Webinar@RISK Unchained Webinar
@RISK Unchained Webinar
 
Assumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfAssumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourself
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
 
Php 2
Php 2Php 2
Php 2
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
 
State Space Search
State Space SearchState Space Search
State Space Search
 
OSMC 2013 | Making monitoring simple? by Michael Medin
OSMC 2013 | Making monitoring simple? by Michael MedinOSMC 2013 | Making monitoring simple? by Michael Medin
OSMC 2013 | Making monitoring simple? by Michael Medin
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listing
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.com
 
Cluto presentation
Cluto presentationCluto presentation
Cluto presentation
 
CIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.comCIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.com
 
Cis 115 Effective Communication / snaptutorial.com
Cis 115  Effective Communication / snaptutorial.comCis 115  Effective Communication / snaptutorial.com
Cis 115 Effective Communication / snaptutorial.com
 

Recently uploaded

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 

Recently uploaded (20)

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 

Exact Real Arithmetic for Tcl

  • 1. Exact Real Arithmetic in Tcl Kevin B. Kenny 22nd Annual Tcl/Tk Conference 21 October 2015
  • 2. Have you ever hadHave you ever had a software problema software problem caused bycaused by floating point roundoff?floating point roundoff?
  • 3. Were you using floating pointWere you using floating point for currency?for currency? Did you learn from that mistake?
  • 4. You havenYou haven’’t had floating-pointt had floating-point precision problems?precision problems? How do you know?How do you know?
  • 5. Do you test your softwareDo you test your software for the accuracy offor the accuracy of floating-point results?floating-point results? How do you know whatHow do you know what the right answers are?the right answers are?
  • 6. Floating point is full of horror storiesFloating point is full of horror stories Many involve the precisionMany involve the precision of intermediate results,of intermediate results, not the input data nornot the input data nor the ultimate answersthe ultimate answers
  • 7. Catastrophic loss of significanceCatastrophic loss of significance LetLet’’s say that we have the equations say that we have the equation A x 2 +B x+C=0 And we want to findAnd we want to find xx. Asking the nearest high-. Asking the nearest high- schooler for how to do it we get told,schooler for how to do it we get told, x= −B±√B 2 −4 A C 2 A Everyone knows that! Brahmagupta published it inEveryone knows that! Brahmagupta published it in A.D. 678! People have used that formula for 1500A.D. 678! People have used that formula for 1500 years!years!
  • 8. Catastrophic loss of significanceCatastrophic loss of significance proc quad1 {a b c} {proc quad1 {a b c} { set d [expr {sqrt($b*$b – 4.*$a*$c)}]set d [expr {sqrt($b*$b – 4.*$a*$c)}] set r0 [expr {(-$b - $d) / (2. * $a)}]set r0 [expr {(-$b - $d) / (2. * $a)}] set r1 [expr {(-$b + $d) / (2. * $a)}]set r1 [expr {(-$b + $d) / (2. * $a)}] return [list $r0 $r1]return [list $r0 $r1] }} quad1 1.0 200.0 -1.5e-12quad1 1.0 200.0 -1.5e-12 →→ -200.0-200.0 1.4210854715202004e-141.4210854715202004e-14 What?What? The correct answer is approximately 7.5The correct answer is approximately 7.5××1010-15-15 !! (The answer:(The answer: -200. - sqrt(400.000000000006)-200. - sqrt(400.000000000006) losesloses all but one bit of the significand.)all but one bit of the significand.)
  • 9. YouYou’’re doing it wrong!re doing it wrong! An experienced numerical analyst will tell you thatAn experienced numerical analyst will tell you that the right way to use the quadratic formula is tothe right way to use the quadratic formula is to write it in a different way:write it in a different way: x= −B±√B2 −4 A C 2 A x= 2C −B±√B 2 −4 A C Experienced numerical analysts are expensive.Experienced numerical analysts are expensive. They can take a long time to solve your problem.They can take a long time to solve your problem. They still make mistakes and miss things.They still make mistakes and miss things. How do you know whenHow do you know when theythey have it right?have it right?
  • 10. A popular idea: variable precisionA popular idea: variable precision Repeat calculations at different precisionsRepeat calculations at different precisions See what changes as precision increases: do weSee what changes as precision increases: do we get the same answer?get the same answer? But consider the problem of finding the limit of:But consider the problem of finding the limit of: x0 = 4.00 x1 = 4.25 xn = 108− 815− 1500 xn−2 xn−1
  • 11. Variable precision: what happens?Variable precision: what happens? # │ float │ double │ exact# │ float │ double │ exact ───┼───────────┼───────────┼──────────────┼───────────┼───────────┼─────────── 1 │ 4.47059 │ 4.47059 │ 4.470591 │ 4.47059 │ 4.47059 │ 4.47059 2 │ 4.64474 │ 4.64474 │ 4.644742 │ 4.64474 │ 4.64474 │ 4.64474 3 │ 4.77053 │ 4.77054 │ 4.770543 │ 4.77053 │ 4.77054 │ 4.77054 4 │ 4.85545 │ 4.85570 │ 4.855704 │ 4.85545 │ 4.85570 │ 4.85570 5 │ 4.90575 │ 4.91085 │ 4.910855 │ 4.90575 │ 4.91085 │ 4.91085 6 │ 4.84165 │ 4.94554 │ 4.945546 │ 4.84165 │ 4.94554 │ 4.94554 7 │ 2.82180 │ 4.96696 │ 4.966967 │ 2.82180 │ 4.96696 │ 4.96696 8 │ -71.03029 │ 4.98004 │ 4.980048 │ -71.03029 │ 4.98004 │ 4.98004 9 │ 111.99020 │ 4.98791 │ 4.987989 │ 111.99020 │ 4.98791 │ 4.98798 10 │ 100.53401 │ 4.99136 │ 4.9927710 │ 100.53401 │ 4.99136 │ 4.99277 11 │ 100.02652 │ 4.96746 │ 4.9956611 │ 100.02652 │ 4.96746 │ 4.99566 12 │ 100.00133 │ 4.42969 │ 4.9973912 │ 100.00133 │ 4.42969 │ 4.99739 13 │ 100.00007 │ -7.81724 │ 4.9984313 │ 100.00007 │ -7.81724 │ 4.99843 14 │ 100.00000 │ 168.93917 │ 4.9990614 │ 100.00000 │ 168.93917 │ 4.99906 15 │ 100.00000 │ 102.03996 │ 4.9994415 │ 100.00000 │ 102.03996 │ 4.99944 16 │ 100.00000 │ 100.09995 │ 4.9996616 │ 100.00000 │ 100.09995 │ 4.99966 17 │ 100.00000 │ 100.00499 │ 4.9998017 │ 100.00000 │ 100.00499 │ 4.99980 18 │ 100.00000 │ 100.00025 │ 4.9998818 │ 100.00000 │ 100.00025 │ 4.99988 IEEE-754 ‘float’gives 1decimalplace, thenexplodes! IEEE-754 ‘double’manages2 decimalplaces,thenexplodes intheexactsameway! Essentially all floating- point systems converge on 100. It's easy to overlook the wrong answer.
  • 12. What if we hadWhat if we had exactexact arithmetic?arithmetic? ● Control the precision of the outputControl the precision of the output ● Compute intermediate results to whatever levelCompute intermediate results to whatever level of precision is neededof precision is needed ● ReplaceReplace ‘‘numbernumber’’ withwith ‘algorithmtocomputethe‘algorithmtocomputethe numbernumber’’
  • 13. Exact arithmetic packageExact arithmetic package TcllibTcllib math::exactmath::exact package. (Pure Tcl.)package. (Pure Tcl.) Numbers (the result of calculations) are TclOO objects.Numbers (the result of calculations) are TclOO objects. Reference counted objects – which is unpleasant.Reference counted objects – which is unpleasant. Created byCreated by math::exact::exactexprmath::exact::exactexpr Can format themselves withCan format themselves with asPrintasPrint andand asFloatasFloat methods.methods. Methods take desired precision. (Precision is the sum ofMethods take desired precision. (Precision is the sum of the number of bits of exponent plus the number of bits ofthe number of bits of exponent plus the number of bits of significand.)significand.)
  • 14. Exact arithmetic - exampleExact arithmetic - example %% package require math::exactpackage require math::exact 1.01.0 %% namespace import math::exact::exactexprnamespace import math::exact::exactexpr %% set r [[exactexpr {exp(pi()*sqrt(163))}] ref]set r [[exactexpr {exp(pi()*sqrt(163))}] ref] ::oo::Obj118::oo::Obj118 %% $r asFloat 108$r asFloat 108 2.62537412640768e172.62537412640768e17 %% $r asFloat 200$r asFloat 200 2.625374126407687439999999999992500725971981e172.625374126407687439999999999992500725971981e17 %% set f [[exactexpr {$r-262537412640768744}] ref]set f [[exactexpr {$r-262537412640768744}] ref] ::oo::Obj125::oo::Obj125 %% $f asFloat 100$f asFloat 100 -7.49927402801814311e-13-7.49927402801814311e-13 %% $f unref$f unref %% $r unref$r unref
  • 15. Exact arithmetic:Exact arithmetic: fixing the quadratic formulafixing the quadratic formula proc exactquad {a b c} {proc exactquad {a b c} { set d [[exactexpr {sqrt($b*$b - 4*$a*$c)}] ref]set d [[exactexpr {sqrt($b*$b - 4*$a*$c)}] ref] set r0 [[exactexpr {(-$b - $d)set r0 [[exactexpr {(-$b - $d) / (2 * $a)}] ref]/ (2 * $a)}] ref] set r1 [[exactexpr {(-$b + $d)set r1 [[exactexpr {(-$b + $d) / (2 * $a)}] ref]/ (2 * $a)}] ref] $d unref$d unref return [list $r0 $r1]return [list $r0 $r1] }}
  • 16. Exact arithmetic:Exact arithmetic: fixing the quadratic formulafixing the quadratic formula set a [[exactexpr 1] ref]set a [[exactexpr 1] ref] set b [[exactexpr 200] ref]set b [[exactexpr 200] ref] set c [[exactexpr {(-3/2) * 10**-12}] ref]set c [[exactexpr {(-3/2) * 10**-12}] ref] lassign [exactquad $a $b $c] r0 r1lassign [exactquad $a $b $c] r0 r1 $a unref; $b unref; $c unref$a unref; $b unref; $c unref puts [list [$r0 asFloat 70] [$r1 asFloat 110]]puts [list [$r0 asFloat 70] [$r1 asFloat 110]] $r0 unref; $r1 unref$r0 unref; $r1 unref -2.000000000000000075e2 7.499999999999999719e-15-2.000000000000000075e2 7.499999999999999719e-15 No manipulation of the formulas! Just ask for 70 “bits” forNo manipulation of the formulas! Just ask for 70 “bits” for the larger root and 110 “bits” for the smaller, and you getthe larger root and 110 “bits” for the smaller, and you get them.them.
  • 17. Major disadvantagesMajor disadvantages ● SLOW – but for generating constants inSLOW – but for generating constants in advance (for use in algorithms or for testing),advance (for use in algorithms or for testing), probably okprobably ok ● No comparison operators (I'll explain…)No comparison operators (I'll explain…) ● No floating point notation on inputNo floating point notation on input ● Ref counting rather than automatic reclamationRef counting rather than automatic reclamation
  • 18. No comparison?No comparison? ● Comparison of reals isn't decidable.Comparison of reals isn't decidable. ● What you can have are operatorsWhat you can have are operators a<ϵ b,a>ϵ b,a=ϵ b,... |b−a|≤ϵ These comparisons are computible. Need a notation for specifying these in expressions. which are allowed to return the wrong answer if
  • 19. No floating point notation?No floating point notation? What doesWhat does 0.33333333333333330.3333333333333333 mean?mean? ● The fractionThe fraction 3333333333333333/10000000000000000, exactly3333333333333333/10000000000000000, exactly representing the string representation?representing the string representation? ● The fractionThe fraction 6004799503160661/18014398509481984,6004799503160661/18014398509481984, exactly representing the internal representation?exactly representing the internal representation? ● The fraction 1/3, which will have string and internalThe fraction 1/3, which will have string and internal representations as above?representations as above? For now, say what you mean! (Suggestions for what'sFor now, say what you mean! (Suggestions for what's reasonable are welcome!)reasonable are welcome!)
  • 20. No garbage collection?No garbage collection? ● The fragile reference problemThe fragile reference problem ● Common to all interfaces between Tcl andCommon to all interfaces between Tcl and managed code systems (e.g., Java, .NET)managed code systems (e.g., Java, .NET) ● Bigger topic than just exact realsBigger topic than just exact reals ● Generic extension for adding fragile referencesGeneric extension for adding fragile references exists. (Implemented originally for TCOM)exists. (Implemented originally for TCOM) http://www.vex.net/~cthuang/counted/http://www.vex.net/~cthuang/counted/
  • 21. How am I doing for time?
  • 22. Want to explore under the hood? (Warning, some mathematics ahead...)
  • 23. Digit streamsDigit streams Digit streams, at first look attractive.Digit streams, at first look attractive. Make each algorithm into a coroutine that returnsMake each algorithm into a coroutine that returns decimal or binary digits.decimal or binary digits. But there is a problem: things get jammed up!But there is a problem: things get jammed up! What is 1/6What is 1/6 ×× 9 in decimal?9 in decimal? (It is 1.5, right?)(It is 1.5, right?) Recall that 1/6 = 0.16666….Recall that 1/6 = 0.16666….
  • 24. 1/61/6 ×× 99 0.90.9 ≤≤ 0.1…0.1… ×× 99 1.8≤1.8≤ NotenoughinformationtooutputadigitNotenoughinformationtooutputadigit 1.441.44 ≤≤ 0.16…0.16… ×× 99 1.53≤1.53≤ Outputtheleadingdigit1Outputtheleadingdigit1 1.494≤1.494≤ 0.166…×0.166…× 99 1.503≤1.503≤ 1.4994≤1.4994≤ 0.1666…×0.1666…× 99 1.5003≤1.5003≤ 1.49994≤1.49994≤ 0.16666…×0.16666…× 99 1.50003≤1.50003≤ Ithinkwe'restuckinaloop.Ithinkwe'restuckinaloop.
  • 25. What about continued fractions?What about continued fractions? x=a+ 1 b+ 1 c+ 1 ⋱ Terminate for all the rationals. Periodic for quadratic surds. The same problem with getting jammed – just more complicated expressions. What is sqrt(2)**2? Sqrt(2) ≤ 1+1/ 2(1.5) Sqrt(2)**2 ≤ 2.25 Sqrt(2) ≥ 1+1/(2+1) (1.333…) Sqrt(2)**2 ≥ 1.777... Sqrt(2) ≥ 1+1/(2+1/2) (1.4) Sqrt(2)**2 ≥ 1.96 Sqrt(2) ≤ 1+1/(2+1/(2+1)) (1.42857…) Sqrt(2)**2 ≤ 2.04… Sqrt(2) ≤ 1+1/(2+1/(2+1/2))) (1.41666…) Sqrt(2)**2 ≤ 2.0069444... Sqrt(2) ≥ 1+1/(2+1/(2+1/(2+1)))) (1.41176…) Sqrt(2)**2 ≥ 1.993... We can never decide whether the a term of the result is 1 or 2!
  • 26. Why do these ideas fail?Why do these ideas fail? ● If numbers are programs, exactIf numbers are programs, exact comparison (equality, forcomparison (equality, for example) is the Halting Problem.example) is the Halting Problem. ● Alan Turing was originally anAlan Turing was originally an analyst, not a logician.analyst, not a logician. ● TuringTuring’’s work on the Haltings work on the Halting Problem was a byproduct of hisProblem was a byproduct of his attempt to put the real numbersattempt to put the real numbers on a sound theoretical footing.on a sound theoretical footing. Exact comparison of real numbersExact comparison of real numbers is not decidable!is not decidable!
  • 27. Use weak comparisonsUse weak comparisons a<ϵ b,a>ϵ b,a=ϵ b,... Allowed to return the wrong answer if |b−a|≤ϵ Weak comparisons are computible.
  • 28. Use redundant representationsUse redundant representations More than one way to write a given numberMore than one way to write a given number Can choose at least one correct way to write aCan choose at least one correct way to write a number using only weak comparisons.number using only weak comparisons.
  • 29. Rational numbers and integerRational numbers and integer vectorsvectors Represent the rational number a/b by the vectorRepresent the rational number a/b by the vector {a b}{a b} (Assume reduction to lowest terms).(Assume reduction to lowest terms). Division by zero is OK, and there is a single pointDivision by zero is OK, and there is a single point at infinity.at infinity. a b b=1 (a/b,1)
  • 30. Möbius transformationsMöbius transformations The matrixThe matrix [a c b d] represents the function a x+c b x+d a IfIf xx>0>0, this is also the, this is also the generalized intervalgeneralized interval [[cc//dd .... aa//bb], moving], moving clockwise on the circleclockwise on the circle a b (c/d ,1) (a/b,1)
  • 31. Composition of transformationsComposition of transformations If f (x)= a x+c b x+d , and g(x)= e x+g f x+h , then f (g(x))= (ae+c f )x+(a g+ch) (be+d f )x+(b g+dh) This is just matrix multiplication: [a c b d][e g f h]=[ae+c f a g+ch be+d f bg+d h]
  • 32. Relation to continued fractionsRelation to continued fractions a+ 1 b+ 1 c+ 1 ⋱ can be written[a 1 1 0]⋅[b 1 1 0]⋅[c 1 1 0]⋅⋯. Real numbers are streams ofReal numbers are streams of matrices.matrices.
  • 33. Bilinear transformationsBilinear transformations We also use 2We also use 2××22××2 tensors. If2 tensors. If xx andand yy are vectors,are vectors, write:write: [a e b f |c g d h]L x R y , meaning a x y+b x+c y+d e x y+f x+g y+h IfIf xx andand yy are vectors,are vectors, AA andand BB are matrices,are matrices, TT is ais a tensor, all of the following are well defined:tensor, all of the following are well defined: A⋅x vector T L x matrix T R y vector A⋅B matrix T L A matrix T R B matrix A⋅T tensor
  • 34. Arithmetic on exact realsArithmetic on exact reals Remembering thatRemembering that [a e b f |c g d h]L x R y means a x y+b x+c y+d e x y+f x+g y+h The following special tensors will be useful.The following special tensors will be useful. [0 1 0 0|1 0 0 1] addition [1 0 0 0|0 0 0 1] multiplication [0 1 0 0|−1 0 0 1] subtraction [0 1 0 0|0 0 1 0] division
  • 35. Expressions are trees of tensors,Expressions are trees of tensors, matrices, vectorsmatrices, vectors ● Special functions and non-integer powers areSpecial functions and non-integer powers are infinite treesinfinite trees ● Lazy evaluation. A node in a special functionLazy evaluation. A node in a special function knows how to instantiate its children and doesknows how to instantiate its children and does so only when it needs to.so only when it needs to. ● Example:Example: √x=[x+1 2 x 2 x+1]⋅[x+1 2 x 2 x+1]⋅[x+1 2x 2 x+1]⋅⋯ ln x=[x−1 x−1 1 x ]⋅∏ n=1 ∞ [n x+n+1 (2n+1)x 2n+1 (n+1)x+n]
  • 36. Sign matricesSign matrices A sign matrix selects half the number circle.A sign matrix selects half the number circle. S−=[0 −1 1 0 ] S∞=[ 1 1 −1 1]S+=[1 0 0 1] S0=[1 −1 1 1 ] Every number's stream begins with a sign matrix.Every number's stream begins with a sign matrix. Always have two choices – decidable.Always have two choices – decidable. Remaining matrices in the stream will have all elementsRemaining matrices in the stream will have all elements nonnegative.nonnegative.
  • 37. Digit matricesDigit matrices ● Digit matrices slice up the nonnegative half-lineDigit matrices slice up the nonnegative half-line D−=[1 0 1 2] D+=[2 1 0 1]D0=[3 1 1 3] 0 1/3 1 3 Each positive number is aEach positive number is a stream of digit matrices.stream of digit matrices. Each digit matrix conveysEach digit matrix conveys ≈≈1 bit of information.1 bit of information.
  • 38. Pulling the streamPulling the stream ● For each possible digit matrixFor each possible digit matrix DD, a matrix, a matrix MM or tensoror tensor TT asks whether it can prove thatasks whether it can prove that DD−1−1 xx is still positive.is still positive. ● If so, it emitsIf so, it emits DD and replaces itself withand replaces itself with DD−1−1 xx.. ● If no digit is emitted, then a matrixIf no digit is emitted, then a matrix MM asks for a digitasks for a digit EE from its argument, multiplies itself by the digit and triesfrom its argument, multiplies itself by the digit and tries again.again. ● A tensorA tensor TT will ask its left and right arguments inwill ask its left and right arguments in succession and replace itself withsuccession and replace itself with TT LL EE oror TT RR EE respectivelyrespectively ● Every digit can be delivered in a finite number of stepsEvery digit can be delivered in a finite number of steps