when e is really dr. ernesto gomez dr. yasha karant dr. keith evan schubert the authors gratefully...

23
When When e e Is Really Is Really Dr. Ernesto Gomez Dr. Ernesto Gomez Dr. Yasha Karant Dr. Yasha Karant Dr. Keith Evan Schubert Dr. Keith Evan Schubert The authors gratefully acknowledg the NSF’s support by CISE 98-107

Upload: augusta-hunt

Post on 24-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 2: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 3: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 4: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 5: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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, …)

Page 6: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 7: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 8: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 9: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

Real ProblemReal Problem

zyx ,,xp xy pyz

ppz

Page 10: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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)

Page 11: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 12: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 13: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 14: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 15: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 16: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 17: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 18: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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));}

Page 19: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 20: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 21: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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

Page 22: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. 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

Page 23: When e Is Really  Dr. Ernesto Gomez Dr. Yasha Karant Dr. Keith Evan Schubert The authors gratefully acknowledge the NSF’s support by CISE 98-10708

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