Upload
madeline-phelps
View
215
Download
0
Embed Size (px)
Citation preview
Floating Point NumbersExpressions
Scanner InputAlgorithms to Programs
Shirley MooreCS 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 - .90.09999999999999998> 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.10.5000000000000001> int d = 2147483647> float e = (float) d> d2147483647> e2.14748365E9> e = e - 21474836460.0> d = d - 21474836461> d = (int) e0
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)> f1.0E38> int g = (int) f> g2147483647> float h = (float) Math.pow(10,39)> hInfinity> float p = (float) Math.pow(10,-50)> p0.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 followingScanner input = new Scanner(System.in);System.out.println(“Enter an integer: “);int n = input.nextInt();nSystem.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
Algorithm for Problem 2.2
Lab 3 – Projectile Motion
• By Thursday– Problem description– Algorithm– Set of test cases