devxlogo

Explore More Methods in the java.lang.Math Package

java.lang.Math methods for Euler's number

The java.lang.Math package ships with several methods related to Euler’s number (e ≈ 2.71828) that are easy to overlook. Two of them, Math.exp(x) and Math.expm1(x), come up all the time in statistics, machine-learning loss functions, interest calculations, and scientific code. In 2026, most hot-path numeric code still ends up calling these static intrinsics because the JIT compiles them down to single CPU instructions on modern JDKs.

public class MathMethods {
    public static void main(String[] args) {
        new MathMethods().proceed();
    }

    private void proceed() {
        // Math.exp returns e^x
        System.out.println(Math.exp(5));
        // Math.expm1 returns e^x - 1, more accurate for small x
        System.out.println(Math.expm1(5));
    }
}

Expected output:

148.4131591025766
147.4131591025766

When to prefer expm1 over exp

For very small values of x, Math.exp(x) - 1 loses precision because you are subtracting two nearly-equal doubles. Math.expm1(x) computes e^x – 1 directly and keeps the significant digits. The same trick applies to Math.log1p(x), which is the partner function for computing log(1 + x) accurately near zero. Reach for the “1p” and “m1” variants in any financial or probabilistic code where small-x behavior matters.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.