lec5 - 1.00 Lecture 5 More on Java Data Types, Control...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.00 Lecture 5 More on Java Data Types, Control Structures Introduction to Methods Reading for next time: Big Java: 7.1-7.5, 7.8 Floating Point Anomalies Anomalous floating point values: Undefined, such as 0.0/0.0: 0.0/0.0 produces result NaN (Not a Number) Any operation involving NaN produces NaN as result Two NaN values cannot be equal Check if number is NaN by using methods: Double.isNaN(double d) or Float.isNAN(int i) Return boolean which is true if argument is NaN Overflow, such as 1.0/0.0: 1.0/0.0 produces result POSITIVE_INFINITY -1.0/0.0 produces result NEGATIVE_INFINITY Same rules, results as for NaN (Double.isInfinite) Underflow, when result is smaller than smallest possible number we can represent Complex, not handled very well (represented as zero) Example public class NaNTest { public static void main(String args) { double a=0.0, b=0.0, c, d; c= a/b; System.out.println("c: " + c); if (Double.isNaN(c)) System.out.println(" c is NaN"); d= c + 1.0; System.out.println("d: " + d); if (Double.isNaN(d)) System.out.println(" d is NaN"); if (c == d) System.out.println("Oops"); else System.out.println("NaN != NaN"); double e= 1.0, f; f= e/a; System.out.println("f: " + f); if (Double.isInfinite(f)) System.out.println(" f is infinite"); } } Doubles as Bad Loop Counters public class Counter { public static void main(String args) { int i= 0; double x= 0.0; while (x <= 10.0) { x += 0.2; i++; if ( i % 10 == 0 || i >= 48) System.out.println(x: " + x + " i: " + i); } } } Doubles as Bad Loop Counters i : 10 x : 1.9999999999999998 i : 20 x : 4.000000000000001 Notice accumulating, i : 30 x : 6.000000000000003 increasing error. Never i : 40 x : 8.000000000000004 use floats or doubles as i : 48 x : 9.599999999999998 loop counters (well, i : 49 x : 9.799999999999997 almost never) i : 50 x : 9.999999999999996 i : 51 x : 10.199999999999996 We went one iteration too many Exercise Create a class AngleTest Loop over angles from min to max by Output the angle Compute and output 1/( max - ) Make sure you get very close to max Input...
View Full Document

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 11

lec5 - 1.00 Lecture 5 More on Java Data Types, Control...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online