when e is really dr. ernesto gomez dr. yasha karant dr. keith evan schubert the authors gratefully...
TRANSCRIPT
When When ee Is Really Is Really Dr. Ernesto GomezDr. Ernesto Gomez
Dr. Yasha KarantDr. Yasha Karant
Dr. Keith Evan SchubertDr. Keith Evan Schubert
The authors gratefully acknowledge the NSF’s support by CISE 98-10708
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
GoalGoal
To Be PerfectTo Be Perfect
FundamentalsFundamentals
What They KnowWhat They Know
What They Don’t KnowWhat They Don’t Know
When That Line Is CrossedWhen That Line Is Crossed
How To Get HelpHow To Get Help
Caution Floating-Point AheadCaution Floating-Point Ahead
FP FP ≠≠Error PropagationError Propagation
Computation LimitsComputation Limits OverflowOverflow UnderflowUnderflow Loss of precisionLoss of precision
Algorithm DesignAlgorithm Design
Test CasesTest Cases
Other GoalsOther Goals
Challenge and excite studentsChallenge and excite students
Give the “flavor” of the disciplineGive the “flavor” of the discipline
Learn to analyze code and explain Learn to analyze code and explain functioningfunctioning
Relations to other fields (Calculus, Relations to other fields (Calculus, Mappings, …)Mappings, …)
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
Floating Point NumbersFloating Point Numbers
IEEE 754 Double PrecisionIEEE 754 Double Precision
S1
Exponent11
Significant (mantissa)52 + hidden bit
1023exponent2.11 tsignificans
64 bits
OverflowUnderflow*
Loss of Precision
machine
machine
x
xxfl
fl
11
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
Real ProblemReal Problem
zyx ,,xp xy pyz
ppz
Oh Say Can You, C?Oh Say Can You, C?
double x, y, z;double x, y, z;
x=4*atan(1);x=4*atan(1);
y=pow(x,pow(2,-52)); y=pow(x,pow(2,-52));
z=pow(y,pow(2,52));z=pow(y,pow(2,52));
// yields z=2.7182818284590451=fl(e)// yields z=2.7182818284590451=fl(e)
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
A Little CalculusA Little Calculus
One way to calculate One way to calculate ee is is
In finite precision, the limit doesn’t make In finite precision, the limit doesn’t make sensesense Have to limit (1/n) to Have to limit (1/n) to , thus n=2, thus n=25252
nnne 11lim
5225221 efl
From From To To ee
Square root is a contraction with fixed Square root is a contraction with fixed point at 1point at 1 fl(sqrt(fl(sqrt())=1.11000001…))=1.11000001… fl(sqrt( fl(sqrt(fl(sqrt( fl(sqrt()) ))=1.010010…)) ))=1.010010… fl(sqrt( fl(sqrt(fl(sqrt(fl(sqrt( fl(sqrt(fl(sqrt()))) ))=1.0010011…)))) ))=1.0010011…
nn
212
Square root is a contraction with fixed Square root is a contraction with fixed point at 1point at 1 fl(sqrt(fl(sqrt())=))=1.11.11000001…1000001… fl(sqrt( fl(sqrt(fl(sqrt( fl(sqrt()) ))=)) ))=1.011.010010…0010… fl(sqrt( fl(sqrt(fl(sqrt(fl(sqrt( fl(sqrt(fl(sqrt()))) ))=)))) ))=1.0011.0010011…0011…
Essentially Essentially
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
Not All Code Is EqualNot All Code Is Equal
Method of Method of
calculating xcalculating xyy
EstimateEstimate
Of Of ee
Digits ofDigits of
““Accuracy”Accuracy”
pow(x,y)pow(x,y)
exp(y*log(x))exp(y*log(x))
for loopfor loop
PowPow
y=pow(pi,pow(2,-52)); y=pow(pi,pow(2,-52));
// equivalent to 52 square roots// equivalent to 52 square roots
z=pow(y,pow(2,52)); z=pow(y,pow(2,52));
// equivalent to 52 squares// equivalent to 52 squares
Not All Code Is EqualNot All Code Is Equal
Method of Method of
calculating xcalculating xyy
EstimateEstimate
Of Of ee
Digits ofDigits of
““Accuracy”Accuracy”
pow(x,y)pow(x,y) 2.71828182845904512.7182818284590451 1616
exp(y*log(x))exp(y*log(x))
for loopfor loop
Pow By Log And ExpPow By Log And Exp
y=pow_log(pi,pow_log(2,-52)); y=pow_log(pi,pow_log(2,-52));
// equivalent to 52 square roots// equivalent to 52 square roots
z=pow_log(y,pow_log(2,52)); z=pow_log(y,pow_log(2,52));
// equivalent to 52 squares// equivalent to 52 squares
double pow_log(double base, double pow){double pow_log(double base, double pow){
return exp(pow*log(base));}return exp(pow*log(base));}
Not All Code Is EqualNot All Code Is Equal
Method of Method of
calculating xcalculating xyy
EstimateEstimate
Of Of ee
Digits ofDigits of
““Accuracy”Accuracy”
pow(x,y)pow(x,y) 2.71828182845904512.7182818284590451 1616
exp(y*log(x))exp(y*log(x)) 2.71828182845903802.7182818284590380 1313
for loopfor loop
For LoopFor Loop
y=pi;y=pi;
for(i=0;i<52;i++)for(i=0;i<52;i++)
y=sqrt(y); // 52 square rootsy=sqrt(y); // 52 square roots
z=y;z=y;
for(i=0;i<52;i++)for(i=0;i<52;i++)
z*=z; // 52 squaresz*=z; // 52 squares
Not All Code Is EqualNot All Code Is Equal
Method of Method of
calculating xcalculating xyy
EstimateEstimate
Of Of ee
Digits ofDigits of
““Accuracy”Accuracy”
pow(x,y)pow(x,y) 2.71828182845904512.7182818284590451 1616
exp(y*log(x))exp(y*log(x)) 2.71828182845903802.7182818284590380 1313
for loopfor loop 2.71828180818247312.7182818081824731 77
OutlineOutline
Educational GoalsEducational Goals
Floating-Point NumbersFloating-Point Numbers
ProblemProblem
A Little CalculusA Little Calculus
Code DifferencesCode Differences
Educational UsesEducational Uses
Why Use It?Why Use It?
Students can easily see the problemStudents can easily see the problem
Good appreciation of “odd behavior” FP Good appreciation of “odd behavior” FP can havecan have
Cannot be fixed with more precisionCannot be fixed with more precision
Ties math courses into CS frameworkTies math courses into CS framework
Introduces calculation of Introduces calculation of eexx
Algorithm differences are apparent in short Algorithm differences are apparent in short codecode