This example shows one of the limitations in Java especially in dealing with Floating Point calculations.
Direction:
- Download the FloatingPoint.java file
- Compile it using the “javac FloatingPoint.java” command (If you dont have the Java SDK, and only have the JRE, then you can just download this compiled class — FloatingPoint.class)
- Run the code using “java FloatingPoint” command
Note:
Java SDK should be installed in your PC in order for the code to be compiled and executed. If you don’t have a JDK, you can download it here. You can also opt not to compile the code (As long as you have downloaded FloatingPoint.class file) but you would at least have a JRE installed on your machine in order to execute FloatingPoint.class.
After running, you would see the following output:
*********************************************************************
IEEE 754 representation of -7.25 = 1100000000011101000000000000000000000000000000000000000000000000
IEEE 754 representation of .00625 = 11111101111001100110011001100110011001100110011001100110011010
0.9200000000000001 = 0.9200000000000001509903313490212894976139068603515625
0.9200000000000002 = 0.9200000000000001509903313490212894976139068603515625
0.9200000000000001 == 0.9200000000000002: true
0.1 = 0.1
0.1 = 0.1000000000000000055511151231257827021181583404541015625
0.2 = 0.200000000000000011102230246251565404236316680908203125
0.3 = 0.299999999999999988897769753748434595763683319091796875
0.4 = 0.40000000000000002220446049250313080847263336181640625
0.1 + 0.2 = 0.3000000000000000444089209850062616169452667236328125
0.1 + 0.3 = 0.40000000000000002220446049250313080847263336181640625
0.1 + 0.2 == 0.3: false
0.1 + 0.3 == 0.4: true
0.1 + 0.1 + 0.1 = 0.30000000000000004
0.1 + 0.1 + 0.1 != 0.3
0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 0.5: true
3 * 0.1 == 0.3: false
1138/1000.0 == 0.001*1138: false
Math.sqrt(2) * Math.sqrt(2) == 2: false
3.0 / 7.0 == (float) (3.0 / 7.0): false
((1.0E50 + -1.0E50) + 17) == (1.0E50 + (-1.0E50 + 17)): false
((2.0 * 0.1) / 3.0) == (2.0 * (0.1 / 3.0)): true
Double.POSITIVE_INFINITY = Infinity
Double.NEGATIVE_INFINITY = -Infinity
Double.NaN = NaN
1.0/0.0 = Infinity
-1.0/0.0 = -Infinity
Math.sqrt(1.0/0.0) = Infinity
1E200 * 1E200 = Infinity
-1E200 * 1E200 = -Infinity
0.0 / 0.0 = NaN
Math.sqrt(-3.0) = NaN
1.0/0.0 - 1.0/0.0 = NaN
0.0 * 1.0/0.0 = NaN
1.0 % 0.0 = NaN
(1.0/0.0) % 1.0 = NaN
1E-200 / 1E200 = 0.0
-1E-200 / 1E200 = -0.0
1 + 2^-53 == 1: true
1 + 2^-53 + 2^-105 == 1: false
Machine precision for double = 2^(-53) + 2^(-105) = 1.1102230246251568E-16
2^-53 + 2^-106 == 2^-53: true
2^53 = 9007199254740992
2^53 + 1 = 9007199254740992
1E-21 = 1.0E-21
Math.pow(10, -21) = 1.0000000000000001E-21
Math.cos(Math.PI/2) = 6.123233995736766E-17
*********************************************************************
Additional explanation is located inside the code’s comments.
Further elaboration of this topic can be viewed at http://www.cs.princeton.edu/introcs/91float/
Java is a very powerful and advanced programming language but still it is not spared from the pitfalls and shortcomings of a common programming language.
We cannot deny the fact that with God as the supreme “Developer” of all; the human mind (which he has created) will still be the best computing mechanism here on earth.
Nevertheless, “I still love Java !!!” (Well, it’s the job that is paying the bills so might as well love it)…
Popularity: 6% [?]