45.1 Math and StrictMath

Java provides Math for performance and StrictMath for reproducibility.


45.1.1 Math vs StrictMath

  • Math: May use platform-specific implementations for better performance; results can vary slightly across platforms
  • StrictMath: Guarantees bit-identical results across all platforms; follows fdlibm (Freely Distributable Math Library)
// Prefer Math for performance
double x = Math.sin(1.0);

// Use StrictMath for reproducible simulations
double y = StrictMath.sin(1.0);

45.1.2 Common Functions

// Exponential and logarithm
double exp = Math.exp(2.0);       // e^2
double log = Math.log(10.0);      // ln(10)
double log10 = Math.log10(100.0); // log₁₀(100) = 2.0

// Trigonometric (radians)
double sin = Math.sin(Math.PI / 2);  // 1.0
double cos = Math.cos(0);            // 1.0
double tan = Math.tan(Math.PI / 4);  // ~1.0

// Inverse trig
double asin = Math.asin(1.0);        // π/2
double atan2 = Math.atan2(1, 1);     // π/4

// Hyperbolic
double sinh = Math.sinh(1.0);
double cosh = Math.cosh(1.0);
double tanh = Math.tanh(1.0);

// Power and roots
double pow = Math.pow(2, 10);        // 1024.0
double sqrt = Math.sqrt(16);         // 4.0
double cbrt = Math.cbrt(27);         // 3.0

45.1.3 Special Values and Constants

double pi = Math.PI;      // π
double e = Math.E;        // Euler's number

// Special handling
double inf = Double.POSITIVE_INFINITY;
double nan = Double.NaN;
boolean isNaN = Double.isNaN(0.0 / 0.0);      // true
boolean isFinite = Double.isFinite(1.0 / 0.0); // false

45.1.4 Accuracy Considerations

  • Functions accurate to ~1 ULP (unit in last place) for most inputs
  • Edge cases (very large/small arguments) may have larger errors
  • For critical applications, validate against reference implementations