Commit e78d905
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
1 file changed
+1
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
23 | | - | |
| 22 | + | |
0 commit comments