Skip to content

Commit e78d905

Browse files
committed
feat: add WrapperDemo2 to demonstrate special Float wrapper behaviors in Java
WHAT was added: - Example program showing how Java’s `Float` wrapper handles: - Division by zero (produces Infinity). - Square root results (normal number vs NaN cases). - Equality checks with `NaN`, `Infinity`, and primitive values. KEY BEHAVIORS DEMONSTRATED: 1. Division by zero (`12.5f/0`) → results in `Float.POSITIVE_INFINITY`. 2. `.isInfinite()` → detects whether a float value is infinite. 3. Comparison with constants: - `b == Float.POSITIVE_INFINITY` → true when dividing positive number by zero. - `b == Float.NEGATIVE_INFINITY` → false in this case (since dividend was positive). 4. `.isNaN()` → correctly identifies "Not a Number" (e.g., result of `0.0f/0.0f`). 5. Comparing with `Float.NaN`: - `x == Float.NaN` → **always false** (by IEEE 754 standard). - `Float.NaN != Float.NaN` → **always true** (NaN is not equal to itself). REAL-WORLD APPLICATIONS: - ✅ Scientific or financial applications: detect invalid computations (e.g., divide by zero). - ✅ Data validation pipelines: filter out NaN and Infinity values before saving or processing. - ✅ Simulation engines: distinguish between valid finite results and overflow/undefined cases. - ✅ Error handling: prevent unexpected crashes by explicitly checking `.isNaN()` or `.isInfinite()`. IMPORTANT NOTES: - Never use `==` to check for `NaN`; always use `.isNaN()`. - Infinity (`+∞` or `-∞`) comparisons with constants (`Float.POSITIVE_INFINITY`, `Float.NEGATIVE_INFINITY`) are valid. - IEEE 754 floating-point standard ensures these special values behave consistently across platforms. RULE OF THUMB: - Use `.isNaN()` and `.isInfinite()` for safe checks. - Don’t rely on `==` for detecting NaN. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 698a805 commit e78d905

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

Section20JAVA.lang.Package/src/WrapperDemo2.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ public class WrapperDemo2 {
22
public static void main(String[] args) {
33
float a =12.5f;
44
Float b =12.5f/0;
5-
65
Float c =(float)Math.sqrt(9); //Square root of 9 is 3. then it is a number.
76

87
System.out.println(b.equals(a));
@@ -20,4 +19,4 @@ public static void main(String[] args) {
2019

2120
System.out.println(Float.NaN!=Float.NaN);
2221
}
23-
}
22+
}

0 commit comments

Comments
 (0)