100 likes | 223 Views
Floating Point Numbers Expressions Scanner Input Algorithms to Programs. Shirley Moore CS 1401 Spring 2013 February 12, 2013. Learning Outcomes. Explain how floating point numbers are represented inside a computer
E N D
Floating Point NumbersExpressionsScanner InputAlgorithms to Programs Shirley Moore CS 1401 Spring 2013 February 12, 2013
Learning Outcomes • Explain how floating point numbers are represented inside a computer • Define what is meant by range and precision of a floating point number representation • Define roundoff error and explain how it can accumulate to produce results with significant errors • Evaluate expressions using operator precedence rules • Use the Java Scanner class to obtain user keyboard input • Solve simple IPO (Input-Processing-Output) programming problems by • Writing a step-by-step algorithm • Implementing the algorithm in Java • Verifying correct output
Real vs. Floating Point Numbers • How many real numbers are there between 0 and 1? • Floating point numbers can be represented in 32 bits (single precision) or 64 bits (double precision). • How many floating point numbers can we represent in 32 bits? • How many floating point numbers can we represent in 64 bits? • How can we represent 0.5 in binary? • How can we represent 1/3 as a decimal fraction? • How can we represent 0.1 in binary? • Converting decimal fractions to binary • http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm
Roundoff Error Examples • Try the following in Dr. Java Interactions: > 1.0 - .9 0.09999999999999998 > 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 0.5000000000000001 > intd = 2147483647 > float e = (float) d > d 2147483647 > e 2.14748365E9 > e = e - 2147483646 0.0 > d = d - 2147483646 1 > d = (int) e 0
Floating Point Number Representation • IEEE Standard 754 Floating Point Numbers • http://steve.hollasch.net/cgindex/coding/ieeefloat.html • What types of errors are the following? > float f = (float) Math.pow(10,38) > f 1.0E38 > intg = (int) f > g 2147483647 > float h = (float) Math.pow(10,39) > h Infinity > float p = (float) Math.pow(10,-50) > p 0.0 • Decimal to floating point converter • http://www.binaryconvert.com/convert_double.html
Expression Evaluation • In the Dr Java Interactions window, try evaluating the following expressions: • 5/3 • 5 % 3 • 5./3. • 5 / 0 • 5./0. • 5 < 6 • 5. < 6. • 3 – 2*5 • (3 - 2)*5 • 3 + .100000000 * .10000000 - 3. • Did you get the results you expected?
Scanner Class • In the Dr. Java Interactions window type the following • Scanner input = new Scanner(System.in); • System.out.println(“Enter an integer: “); • intn = input.nextInt(); • n • System.out.println(“Enter a real number: “); • double r = input.nextDouble(); • r
Programming Exercise • Now let’s try a complete program: Problem 2.2, page 75. 2.2 (Compute the volume of a cylinder) Write a program that reads in the radius and length of a cylinder and computes the area of the base and the volume of the cylinder using the following formulas: area = π * radius * radius volume = area * length
Lab 3 – Projectile Motion • By Thursday • Problem description • Algorithm • Set of test cases