Skip to content

Commit 97d58c2

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 5176e8e commit 97d58c2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Section20JAVA.lang.Package/src/MathMethodsUseIt.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ public static void main(String[] args) {
1111
System.out.println(Math.cbrt(27));
1212

1313
/*
14-
1514
int i=Integer.MIN_VALUE;
15+
1616
//i--;
1717
System.out.println(i);
1818
1919
System.out.println("Exact Decrement: ");
2020
System.out.println(Math.decrementExact(7));
2121
System.out.println(Math.decrementExact(Integer.MIN_VALUE));
22-
2322
*/
2423

2524
System.out.println("Exponent Value in Floating Point Rep. :");
2625
System.out.println(Math.getExponent(123456));
2726

28-
//Mantisain Exponent form mai store hota hai. Power of 10.
27+
//Maintain Exponent form mai store hota hai.
28+
//Power of 10.
2929

3030
System.out.println("Round Division: ");
3131
System.out.println(Math.floorDiv(50, 9));
@@ -39,9 +39,11 @@ public static void main(String[] args) {
3939
System.out.println("Maximum: ");
4040
System.out.println(Math.max(100, 50));
4141

42-
System.out.println("Tan :"); //Trigonametric fun it take radian value
42+
System.out.println("Tan :");
43+
//Trigonometric fun it takes radian value.
44+
4345
System.out.println(Math.tan(45*Math.PI/180));
44-
//Computaion on Floting number
46+
//Computation on Floating number.
4547

4648
System.out.println("Convert To Radians :");
4749
System.out.println(Math.toRadians(90));
@@ -64,4 +66,4 @@ public static void main(String[] args) {
6466
System.out.println("Next Float Value :");
6567
System.out.println(Math.nextAfter(12.5, 13));
6668
}
67-
}
69+
}

0 commit comments

Comments
 (0)