29/07/2011 Natural Numbers and Integers Big Ideas in Numerical Methods MEI Conference 2011 Taking some ideas from NM course a little further • How computers store numbers, shedding a little more light on problems with relative error • Interpolating curves – different forms such as Hermite and Bezier curves Real‐world computer number representation • Computers can only represent a finite number of different values. • As a result computers cannot represent any of the natural numbers, integers or real numbers exactly and completely. • An n‐bit computer can only represent 2n distinct values of each set. • Natural numbers can be in the range [0, 232 – 1]. These are known in computing as unsigned int. • Numbers in the range [‐(231), 231 – 1] can are known in computing as int. • In maths, natural numbers are a subset of the integers. • In computing unsigned int is not a subset of int and int is not a subset of unsigned int! Overflow • Overflow is a term used to describe what occurs when a computation generates a results outside the range of the representation in use. • For natural numbers/unsigned int, starting from 0 and incrementing once per frame, with a frame rate of 100 frames per second results in overflow after about 1.4 years. • Negative overflow is a more common occurrence in unsigned int Real Numbers • In mathematics we know that the size of the real numbers is even greater than that of the integers or the natural numbers. • A 32‐bit computer still can only represent 232 different real numbers. • There are two different ways that computers do this, fixed point representation and floating point representation. 1 29/07/2011 Fixed Point Floating Point Numbers (181) • Example for an 8‐bit computer. We might have 4‐dot‐4 representation • This is analogous to scientific notation. It has the form mantissa x 10exponent • ….but with some extra rules • The mantissa has a fixed number of fractional digits. • The exponent has a fixed number of digits. • The mantissa and exponent each have individual signs 23 22 21 20 . 2‐1 2‐2 2‐3 2‐4 • The standard nomenclature for a fixed point format is “M‐dot‐N” where M is the number of places before the decimal point and N is the number of places after it. Conversion between Real and Fixed Point • To convert a real number, r, to M‐dot‐N format: Let F = round (2Nr) Take binary form of F Insert decimal point after M digits • Examples: 4.5 to 4‐dot‐4 7.3 to 4‐dot‐4 Binary floating point numbers • In binary our restricted scientific notation would become Sign M x Mantissa x 2Sign E x Exponent Where Mantissa is a 1‐dot‐M fixed point number, Exponent is an E‐bit integer and Sign M and Sign E are independent bits giving the signs of the mantissa and exponent. This means the format involves M + 1 + E + 2 = M + E + 3 bits. Limits of fixed point • For a 32 bit computer 16‐dot‐16 is representative. Given that one bit is used for +/‐ this means that that maximum representable value is 32767 minimum representable value is about ‐32768 distance between adjacent representable values about 1.5 x 10‐5 Example 8‐bit, M = 3, E = 2 • A number in this form might be 1.010 x 2‐11 ` This is 1.25 x 2‐3 In this system the maximum representable value is 1.875 x 23 = 15 and the smallest value is ‐15. The smallest positive value is 1.000 x 2‐3 = 0.125. The smallest gap between consecutive values is 0.125 x 2‐3 = 0.015625 2 29/07/2011 32‐bit M = 23, E = 8 • Here the maximum and minimum values are +/‐ 3.402823466 x 1038 The difference between a value and it’s nearest neighbour is about 2‐23 which is approximately 6 x 10‐8. Piecewise Linear • Piecewise linear functions clearly give rise to discontinuities in the derivative at the interpolating points. So the greatest error in storing a real number is around 3 x 10‐8 Note that programs like Excel actually achieve much greater accuracy than this by use of Curves • In the Numerical Methods course, the Lagrange Polynomial and the Newton Interpolating Polynomial are the given methods for fitting a curve to points. • In the real world the Lagrange polynomial is fine for interpolating small data sets. • One problem with Lagrange and Newton in computer animation is that the function depends on all the points, so if an animator tweaks their values throughout the development the whole function needs to be recalculated. • Lagrange polynomials also run into numerical problems with larger and larger numbers of points. The joining problem • Suppose you have two points and two specified gradients at each point. Curves The joining problem • Trying to build a single parametric curve by using all the points is not always a productive method • Let’s return to the idea of piecewise equations… • You would like to join these with a curve that has that gradient at each point. 3 29/07/2011 The joining problem Bezier Curves • Bézier curves were widely publicized in 1962 by the French engineer Pierre Bézier, who used them to design automobile bodies. • The curves were first developed in 1959 by Paul de Casteljau The joining problem Quadratic Bezier Curves • Start with three points Demonstration • http://staff.www.ltu.se/~peppar/presentation s/bibdc961114/misc_applets/ParamCurve/ • http://www.rose‐ hulman.edu/~finn/CCLI/Applets/CubicHermite Applet.html Cubic Bezier Curves • Start with four points 4 29/07/2011 • http://en.wikipedia.org/wiki/B%C3%A9zier_cu rve • http://www.geogebra.org/en/upload/files/en glish/oldknow/GGB4a_Bezier_curves.html 5