File: p35.java

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (47 lines) | stat: -rw-r--r-- 1,786 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// From The Java Language Specification, page 35

class Test {

        public static void main(String[] args) {

                // An example of overflow:
                double d = 1e308;
                System.out.print("overflow produces infinity: ");
                System.out.println(d + "*10==" + d*10);

                // An example of gradual underflow:
                d = 1e-305 * Math.PI;
                System.out.print("gradual underflow: " + d + "\n      ");
                for (int i = 0; i < 4; i++)
                        System.out.print(" " + (d /= 100000));
                System.out.println();

                // An example of NaN:
                System.out.print("0.0/0.0 is Not-a-Number: ");
                d = 0.0/0.0;
                System.out.println(d);

                // An example of inexact results and rounding:
                System.out.print("inexact results with float:");
                for (int i = 0; i < 100; i++) {
                        float z = 1.0f / i;
                        if (z * i != 1.0f)
                                System.out.print(" " + i);
                }
                System.out.println();

                // Another example of inexact results and rounding:
                System.out.print("inexact results with double:");
                for (int i = 0; i < 100; i++) {
                        double z = 1.0 / i;
                        if (z * i != 1.0)
                                System.out.print(" " + i);
                }
                System.out.println();

                // An example of cast to integer rounding:
                System.out.print("cast to int rounds toward 0: ");
                d = 12345.6;
                System.out.println((int)d + " " + (int)(-d));
        }
}