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

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

View Full Document Right Arrow Icon
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)
Image of page 1

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

View Full Document Right Arrow Icon
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); } } }
Image of page 2
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 θ min = 0.1, θ max = 4.0, θ = 0.1 How close do you get?
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern