Living with floting-point numers Stndrd normlized representtion (sign + frction + exponent): Approximte computtions Rnges of vlues: Representtions for:, +, +0, 0, NN (not numer) Jordi Cortdell Deprtment of Computer Science Be creful when operting with rel numers: doule x, y; cin >> x >> y; // 1.1 3.1 cout.precision(20); cout << x + y << endl; // 4.2000000000000001776 Single nd doule-precision FP numers flot: doule: 1 sign 1. 51 50 0 2 e 1023 or 1 sign 52 1 + 52 i 2 i 2 e 1023 i=1 Introduction to Progrmming Dept. CS, UPC 2 Compring floting-point numers Comprisons: = + c; if ( == c) Allow certin tolernce for equlity comprisons: if (expr1 == expr2) // Wrong! // my e flse if (s(expr1 expr2) < 0.000001) // Ok! Not every numer cn e represented. Exmple: 0.15 is stored s 0.149999999999999994448884876874 Introduction to Progrmming Dept. CS, UPC 3 Introduction to Progrmming Dept. CS, UPC 4
Root of continuous function Root of continuous function Bolzno s theorem: Let f e rel-vlued continuous function. Let nd e two vlues such tht < nd f() f() < 0. Then, there is vlue c [,] such tht f(c)=0. Design function tht finds root of continuous function f in the intervl [, ] ssuming the conditions of Bolzno s theorem re fulfilled. Given precision ( ), the function must return vlue c such tht the root of f is in the intervl [c, c+ ]. c c Introduction to Progrmming Dept. CS, UPC 5 Root of continuous function Strtegy: nrrow the intervl [, ] y hlf, checking whether the vlue of f in the middle of the intervl is positive or negtive. Iterte until the width of the intervl is smller. Introduction to Progrmming Dept. CS, UPC 6 Root of continuous function // Pre: f is continuous, < nd f()*f() < 0. // Returns c [,] such tht root exists in the // intervl [c,c+ ]. // Invrint: root of f exists in the intervl [,] Introduction to Progrmming Dept. CS, UPC 7 Introduction to Progrmming Dept. CS, UPC 8
Root of continuous function Root of continuous function doule root(doule, doule, doule epsilon) { while ( > epsilon) { doule c = ( + )/2; if (f() f(c) <= 0) = c; else = c; return ; // A recursive version doule root(doule, doule, doule epsilon) { if ( <= epsilon) return ; doule c = ( + )/2; if (f() f(c) <= 0) return root(,c,epsilon); else return root(c,,epsilon); Introduction to Progrmming Dept. CS, UPC 9 The Newton-Rphson method Introduction to Progrmming Dept. CS, UPC 10 The Newton-Rphson method A method for finding successively pproximtions to the roots of rel-vlued function. The function must e differentile. Introduction to Progrmming Dept. CS, UPC 11 Introduction to Progrmming Dept. CS, UPC 12
The Newton-Rphson method Squre root (using Newton-Rphson) Clculte Find the zero of the following function: where Recurrence: source: http://en.wikipedi.org/wiki/newton s_method Introduction to Progrmming Dept. CS, UPC 13 Squre root (using Newton-Rphson) // Pre: >= 0 // Returns x such tht x 2 - < doule squre_root(doule ) { doule x = 1.0; // Mkes n initil guess // Itertes using the Newton-Rphson recurrence while (s(x x ) >= epsilon) x = 0.5 (x + /x); return x; Introduction to Progrmming Dept. CS, UPC 15 Introduction to Progrmming Dept. CS, UPC 14 Squre root (using Newton-Rphson) Exmple: squre_root(1024.0) x 1.00000000000000000000 512.50000000000000000000 257.24902439024390332634 130.61480157022683101786 69.227324054488946103447 42.009585631008270922848 33.192487416854376647279 32.021420905000240964000 32.000007164815897908738 32.000000000000802913291 Introduction to Progrmming Dept. CS, UPC 16
Approximting definite integrls Approximting definite integrls There re vrious methods to pproximte definite integrl: The pproximtion is etter if severl intervls re used: The trpezoidl method pproximtes the re with trpezoid: Introduction to Progrmming Dept. CS, UPC 17 Approximting definite integrls Introduction to Progrmming Dept. CS, UPC 18 Approximting definite integrls // Pre: >=, n > 0 // Returns n pproximtion of the definite integrl of f // etween nd using n intervls. doule integrl(doule, doule, int n) { doule h = ( )/n; h doule s = 0; for (int i = 1; i < n; ++i) s = s + f( + i h); return (f() + f() + 2 s) h/2; Introduction to Progrmming Dept. CS, UPC 19 Introduction to Progrmming Dept. CS, UPC 20
Monte Crlo methods Algorithms tht use repeted genertion of rndom numers to perform numericl computtions. The methods often rely on the existence of n lgorithm tht genertes rndom numers uniformly distriuted over n intervl. In C++ we cn use rnd(), tht genertes numers in the intervl [0, RAND_MAX) Introduction to Progrmming Dept. CS, UPC 21 Approximting Approximting Let us pick rndom point within the unit squre. Q: Wht is the proility for the point to e inside the circle? A: The proility is /4 Algorithm: Generte n rndom points in the unit squre Count the numer of points inside the circle (n in ) Approximte /4 n in /n Introduction to Progrmming Dept. CS, UPC 22 Approximting #include <cstdli> // Pre: n is the numer of generted points // Returns n pproximtion of using n rndom points doule pprox_pi(int n) { int nin = 0; doule rndmx = doule(rand_max); for (int i = 0; i < n; ++i) { doule x = rnd()/rndmx; doule y = rnd()/rndmx; if (x x + y y < 1.0) nin = nin + 1; return 4.0 nin/n; n 10 3.200000 100 3.120000 1,000 3.132000 10,000 3.171200 100,000 3.141520 1,000,000 3.141664 10,000,000 3.141130 100,000,000 3.141692 1,000,000,000 3.141604 Introduction to Progrmming Dept. CS, UPC 23 Introduction to Progrmming Dept. CS, UPC 24
Generting rndom numers in n intervl Monte Crlo pplictions: exmples Assume rnd() genertes rndom nturl numer r in the intervl [0, R). Domin Intervl Rndom numer R [0,1) Τ r R R [0, ) rτr R [, ) + rτr Z [0, ) r mod Z [, ) + r mod ( ) Note: Be creful with integer divisions when delivering rel numers (enforce rel division). Determine the pproximte numer of lttice points in sphere of rdius r centered in the origin. Determine the volume of 3D region R defined s follows: A point P = (x, y, z) is in R if nd only if x 2 + y 2 + 2z 2 100 nd 3x 2 + y 2 + z 2 150. And mny other ppliction domins: Mthemtics, Computtionl Physics, Finnces, Simultion, Artificil Intelligence, Gmes, Computer Grphics, etc. Introduction to Progrmming Dept. CS, UPC 25 Exmple: intersection of two odies Determine the volume of 3D region R defined s follows: A point P = (x, y, z) is in R if nd only if x 2 + y 2 + 2z 2 100 nd 3x 2 + y 2 + z 2 150. The intersection of the two odies is inside rectngulr cuoid C, with center in the origin, such tht: x 2 50 y 2 100 z 2 50 The volume of the cuoid is: Introduction to Progrmming Dept. CS, UPC 26 Exmple: intersection of two odies // Returns n estimtion of the volume of the // intersection of two odies with n rndom smples. doule volume_intersection(int n) { int nin = 0; doule s50 = sqrt(50)/rand_max; // scling for numers in [0,sqrt(50)) doule s10 = 10.0/RAND_MAX; // scling for numers in [0,10) // Generte n rndom smples for (int i = 0; i < n; ++i) { // Generte rndom point inside the cuoid doule x = s50 rnd(); doule y = s10 rnd(); doule z = s50 rnd(); // Check whether the point is inside the intersection if (x x + y y + 2 z z <= 100 nd 3 x x + y y + z z <= 150) ++nin; Volume C = 2 50 2 10 2 50 = 4000 return 4000.0 nin/n; Introduction to Progrmming Dept. CS, UPC 27 Introduction to Progrmming Dept. CS, UPC 28
Exmple: intersection of two odies Volume 2474.56 Summry Approximte computtions is resort when no exct solutions cn e found numericlly. Intervls of tolernce re often used to define the level of ccurcy of the computtion. Rndom smpling methods cn e used to sttisticlly estimte the result of some complex prolems. numer of smples Introduction to Progrmming Dept. CS, UPC 29 Introduction to Progrmming Dept. CS, UPC 30