Skip to content

Commit 3d3e28f

Browse files
committed
feat: demonstrate usage of Math and StrictMath utility methods in Java
WHAT was added: - Implemented a demo program `MathMethodsUseIt` to showcase key methods of the `Math` and `StrictMath` classes. - Covered a wide range of mathematical operations including absolute value, cube root, exponent handling, division, logarithms, trigonometric functions, conversions, random numbers, powers, and next representable floating values. WHY this matters: - The `Math` and `StrictMath` classes provide core mathematical functions in Java, critical for scientific computing, engineering applications, finance, and general-purpose programming. - This program acts as a practical reference to understand the difference between `Math` (platform-dependent optimizations) and `StrictMath` (bitwise reproducible results across platforms). KEY METHODS DEMONSTRATED: 1. **abs() / StrictMath.abs()** - Returns absolute value (positive magnitude). - Useful for handling distances, financial transactions, or error values. 2. **cbrt()** - Computes cube root, e.g., `Math.cbrt(27) = 3.0`. - Used in geometry and 3D computations. 3. **decrementExact()** - Safely decrements with overflow checks. - Throws `ArithmeticException` if result goes below `Integer.MIN_VALUE`. 4. **getExponent()** - Extracts the exponent of a floating-point number in base 2 representation. - Useful in floating-point analysis. 5. **floorDiv()** - Integer division with flooring semantics. - Avoids surprises with negative numbers compared to `/`. 6. **exp() & log10()** - Exponential and logarithmic functions for growth/decay and scientific calculations. 7. **max()** - Returns maximum of two numbers (utility for comparisons). 8. **tan(), toRadians(), toDegrees()** - Trigonometric and angle conversion utilities. - Converts between degrees and radians for geometry/graphics. 9. **random()** - Returns a random `double` in [0,1). - Multiplying scales to desired range (e.g., 0–1000). 10. **pow()** - Raises numbers to a power (e.g., `2^3 = 8`). - Fundamental in exponential growth, cryptography, and simulations. 11. **nextAfter()** - Returns the next floating-point number toward a target. - Useful for precision-sensitive calculations (numerical analysis). StrictMath vs Math: - `Math`: May use platform-specific optimizations (faster but results can vary). - `StrictMath`: Produces reproducible results across all JVMs (IEEE 754 compliant). REAL-WORLD APPLICATIONS: - **Finance**: absolute values, rounding, logarithms, and exponentials for interest calculations. - **Engineering**: trigonometric functions for simulations and design. - **Data Science**: powers, logs, random number generation for statistics and machine learning. - **Graphics**: radians/degrees conversion, trigonometric functions for rendering. - **Numerical computing**: nextAfter(), getExponent() help in floating-point error analysis. BEST PRACTICES: - Use `StrictMath` where deterministic cross-platform results are required. - Prefer `Math.random()` only for simple randomness; for secure randomness use `SecureRandom`. - Use `floorDiv()` instead of `/` when working with negative integers to avoid surprises. KEYWORDS: Math, StrictMath, Java utility methods, trigonometry, logarithm, floating-point precision, random numbers . 1. Math.abs(x) → Negative number ka absolute value deta hai. 2. StrictMath.abs(x) → Math ke jaisa hi hai, but thoda zyada precise float/double ke liye. 3. Math.cbrt(x) → Cube root nikalta hai. 4. Math.decrementExact(x) → x-1 karta hai, lekin agar overflow hoga to Exception throw karega. 5. Math.getExponent(x) → Floating point number ka exponent (base-2 representation). 6. Math.floorDiv(a,b) → Division ka result round karke (towards -infinity) return karta hai. 7. Math.exp(x) → e^x (Euler’s number power x). 8. Math.log10(x) → Base-10 logarithm nikalta hai. 9. Math.max(a,b) → Dono me se bada number return karega. 10. Math.tan(angle) → Angle (radians me) ka tangent value deta hai. 11. Math.toRadians(degree) / Math.toDegrees(radian) → Conversion between degrees ↔ radians. 12. Math.random() → [0,1) ke beech ek random double deta hai (multiply karke range adjust karte hain). 13. Math.pow(a,b) → a^b power calculation. 14. Math.nextAfter(x,y) → x ke baad floating-point number jo y ki taraf move karta hai. 15. StrictMath bhi Math jaisa hi hai but results platform-independent hote hain. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 97d58c2 commit 3d3e28f

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

Section20JAVA.lang.Package/src/MathMethodsUseIt.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ public static void main(String[] args) {
6767
System.out.println(Math.nextAfter(12.5, 13));
6868
}
6969
}
70+

0 commit comments

Comments
 (0)