CS137 Lecture Notes - Lecture 8: Double-Precision Floating-Point Format, Scientific Notation, Approximation Error

58 views3 pages

Document Summary

Integer types float x = 1. 0f; double y = 1. 0: #include int main () // defaults to double double x = -2. 6302e30; printf( %d\n , sizeof(double)); printf( %. 2e\n , x) printf( %g\n , x); return 0; Provides an approximate representation for real numbers. Representation error: r: real number to represent. , 2: p: approximate representation, absolute error = |p-r| Can be large if r is small: care is required when. Eg: if (x==y) risky if (x-y < 0. 0001 && y-x < 0. 0001) safer if (fabs(x-y)<0. 0001) Eg: if (x== 0. 0) if (x == 1. 0) . Round-off error example: #include int main () double x = 5. 0/6. 0; double y = 1. 0/2. 0; double z = 1. 0/3. 0; if (x-y) == z) printf( yes\n ); print ( %g\n , (x-y)-z); else printf( no\n ); return 0; Types (double or int) of program constants are inferred from the literals : eg:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents