12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

computational science basics 271.5.4 Determine Your Machine PrecisionWrite a program to determine the machine precision ɛ m of your computer system(within a factor of 2 or better). A sample pseudocode is✞✝eps = 1.begin do N timeseps = eps/2. / / Make smallerone = 1. + eps / / Write loop number, one , epsend doA Java implementation is given in Listing 1.5, while a more precise one isByteLimit.java on the instructor’s CD.✞// Limits . java : Determines machine precisionpublic class Limits {public static void main ( String [] args ) {final int N = 60;int i;double eps = 1. , onePlusEps ;for ( i = 0; i < N; i=i + 1) {eps = eps/2.;onePlusEps = 1. + eps ;System . out . println ("onePlusEps = " +onePlusEps+" , eps = "+eps ) ;}✝} }☎☎Listing 1.5 The code Limits.java determines machine precision within a factor of 2. Notehow we skip a line at the beginning of each class or method and how we align the closingbrace vertically with its appropriate key word (in italics).1. Determine experimentally the precision of single-precision floats.2. Determine experimentally the precision of double-precision floats.To print out a number in decimal format, the computer must make a conversionfrom its internal binary format. This not only takes time, but unless the number isa power of 2, there is a concordant loss of precision. So if you want a truly preciseindication of the stored numbers, you should avoid conversion to decimals andinstead print them out in octal or hexadecimal format (printf with \0NNN).1.6 Problem: Summing SeriesA classic numerical problem is the summation of a series to evaluate a function. Asan example, consider the infinite series for sin x:sin x = x − x33! + x55! − x7+ ··· (exact).7!−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!