Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 20 Programming with float and double Need for understanding how accurate these variables are.

Similar presentations


Presentation on theme: "Lecture 20 Programming with float and double Need for understanding how accurate these variables are."— Presentation transcript:

1 Lecture 20 Programming with float and double Need for understanding how accurate these variables are

2 Method to compute square root ourSqrt1: bisection method updates lower and upper bounds termination is tricky reduce an initial interval of length 1 to final interval of length about 1.0e-6 in about 20 steps reason: 10 6 is approximately 2 20

3 Much faster method Iterate with just one estimate s Think of s and x/s as being lengths of sides of a rectangle with area s*x/s = x Since we want the rectangle to be “more square”, let’s set the new s to (s + x/s)/2 Very fast: full double accuracy in only about 4 steps “Quadratic convergence”: number of correct digits doubles each step

4 Approximating a derivative Definition of derivative is –lim h  0 [f(x+h) – f(x)]/h Try computing this for h taking the values 0.1, 0.01, 0.001, …., 1e-20 The computations for very small h give garbage because so many digits cancel out in the subtraction Best results are obtained for medium sized h If f is sin, then the derivative is cos, so we can call Math.cos to compare the true answer

5 Graphics When the system needs to redisplay a window or window component, it calls paintComponent Therefore, when we draw graphics on a swing component such as JPanel or JLabel or a user- defined subclass of one of these, we must put these statements inside an overriding paintComponent method Our program should not call paintComponent. Only the system calls it. When we want to make changes to a graphical display, we have to tell the system to call the overriding paintComponent. We do this by calling repaint().

6 Demos TestSwingCommonFeatures TestFigurePanel TestMessagePanel DisplayClock –change from a still to a moving clock MoveMessageDemo ClockAnimation ButtonDemo CheckBoxDemo RadioButtonDemo TextFieldDemo TextAreaDemo ComboBoxDemo ScrollBarDemo etc


Download ppt "Lecture 20 Programming with float and double Need for understanding how accurate these variables are."

Similar presentations


Ads by Google