ZetCode

Java Math Class

Last modified: April 13, 2025

The java.lang.Math class provides static methods for performing basic numeric operations. These include exponential, logarithmic, trigonometric, and rounding functions. The Math class cannot be instantiated as all its methods are static.

Math class methods work with primitive numeric types like double and int. For precision-sensitive applications, consider using BigDecimal instead. The class also defines two important constants: Math.PI and Math.E.

Math Class Overview

The Math class contains methods for basic arithmetic, trigonometry, exponents, logarithms, rounding, and random number generation. All methods are static and thread-safe. The class is part of java.lang package so it's automatically imported.

public final class Math {
    public static final double E = 2.7182818284590452354;
    public static final double PI = 3.14159265358979323846;
    
    // Basic operations
    public static int abs(int a) {...}
    public static double sqrt(double a) {...}
    
    // Trigonometric functions
    public static double sin(double a) {...}
    public static double cos(double a) {...}
    
    // Exponential and logarithmic
    public static double exp(double a) {...}
    public static double log(double a) {...}
    
    // Rounding
    public static long round(double a) {...}
    public static double ceil(double a) {...}
    
    // Random numbers
    public static double random() {...}
}

The code above shows some key methods and constants from the Math class. These methods provide fundamental mathematical operations that are commonly needed in programming.

Basic Arithmetic Operations

The Math class provides methods for basic arithmetic operations like absolute value, maximum/minimum values, and square roots. These methods are optimized for performance and handle edge cases properly.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Absolute value
        int absValue = Math.abs(-15);
        System.out.println("Absolute value of -15: " + absValue);
        
        // Maximum and minimum
        int max = Math.max(10, 20);
        int min = Math.min(10, 20);
        System.out.println("Max of 10 and 20: " + max);
        System.out.println("Min of 10 and 20: " + min);
        
        // Square root
        double sqrt = Math.sqrt(25);
        System.out.println("Square root of 25: " + sqrt);
        
        // Power
        double power = Math.pow(2, 3);
        System.out.println("2 raised to power 3: " + power);
    }
}

This example demonstrates basic arithmetic operations. Math.abs returns the absolute value, Math.max/min compare values, Math.sqrt calculates square roots, and Math.pow handles exponents. All methods are straightforward to use.

Trigonometric Functions

The Math class provides standard trigonometric functions including sine, cosine, and tangent. These methods take angles in radians. For degree conversion, use Math.toRadians or Math.toDegrees.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Convert 30 degrees to radians
        double radians = Math.toRadians(30);
        System.out.println("30 degrees in radians: " + radians);
        
        // Trigonometric functions
        double sinValue = Math.sin(radians);
        double cosValue = Math.cos(radians);
        double tanValue = Math.tan(radians);
        
        System.out.println("Sine of 30°: " + sinValue);
        System.out.println("Cosine of 30°: " + cosValue);
        System.out.println("Tangent of 30°: " + tanValue);
        
        // Inverse trigonometric functions
        double asinValue = Math.asin(sinValue);
        System.out.println("Arcsine of sin(30°): " + 
                          Math.toDegrees(asinValue) + "°");
    }
}

This example shows trigonometric operations. We first convert degrees to radians, then calculate sine, cosine, and tangent. The inverse functions return results in radians which we convert back to degrees for readability.

Exponential and Logarithmic Functions

The Math class provides methods for exponential and logarithmic calculations. Math.exp computes e^x, while Math.log computes natural logarithms (base e). For base 10 logarithms, use Math.log10.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Exponential function
        double expValue = Math.exp(1);
        System.out.println("e^1: " + expValue);
        System.out.println("Math.E: " + Math.E);
        
        // Natural logarithm
        double logValue = Math.log(Math.E);
        System.out.println("ln(e): " + logValue);
        
        // Base 10 logarithm
        double log10Value = Math.log10(100);
        System.out.println("log10(100): " + log10Value);
        
        // Combining operations
        double result = Math.pow(2, Math.log10(100));
        System.out.println("2^log10(100): " + result);
    }
}

This example demonstrates exponential and logarithmic functions. We show the relationship between Math.exp and Math.log, and how they relate to the constant Math.E. The example also shows how to combine these operations with other Math methods.

Rounding Methods

The Math class provides several rounding methods with different behaviors. Math.round performs standard rounding, while Math.ceil always rounds up and Math.floor always rounds down.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double num1 = 3.4;
        double num2 = 3.6;
        double num3 = -3.4;
        
        // Standard rounding
        System.out.println("Round 3.4: " + Math.round(num1));
        System.out.println("Round 3.6: " + Math.round(num2));
        
        // Ceiling (round up)
        System.out.println("Ceil 3.4: " + Math.ceil(num1));
        System.out.println("Ceil -3.4: " + Math.ceil(num3));
        
        // Floor (round down)
        System.out.println("Floor 3.6: " + Math.floor(num2));
        System.out.println("Floor -3.4: " + Math.floor(num3));
        
        // Truncate using casting
        System.out.println("Truncate 3.6: " + (int) num2);
    }
}

This example shows different rounding approaches. Math.round follows standard rounding rules, while Math.ceil and Math.floor always round in specific directions. The example also shows how casting can be used for truncation.

Random Number Generation

The Math.random method generates pseudorandom numbers between 0.0 (inclusive) and 1.0 (exclusive). For more control over random numbers, consider using the Random class from java.util package.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Simple random number
        double random1 = Math.random();
        System.out.println("Random between 0 and 1: " + random1);
        
        // Random number in range
        int min = 10;
        int max = 20;
        int randomInRange = (int) (Math.random() * (max - min + 1)) + min;
        System.out.println("Random between 10 and 20: " + randomInRange);
        
        // Multiple random numbers
        System.out.println("\nFive random numbers:");
        for (int i = 0; i < 5; i++) {
            System.out.println(Math.random());
        }
    }
}

This example demonstrates random number generation. We show how to get a simple random number and how to scale it to a specific range. The formula for range scaling is important for practical applications of random numbers.

Mathematical Constants

The Math class provides two important mathematical constants: Math.PI (π) and Math.E (base of natural logarithms). These constants are defined with high precision and are useful in many mathematical calculations.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Circle calculations using PI
        double radius = 5.0;
        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * Math.pow(radius, 2);
        
        System.out.println("Circle with radius 5:");
        System.out.println("Circumference: " + circumference);
        System.out.println("Area: " + area);
        
        // Exponential growth using E
        double initial = 100;
        double rate = 0.05;
        double time = 10;
        double finalAmount = initial * Math.exp(rate * time);
        
        System.out.println("\nExponential growth:");
        System.out.println("Initial: " + initial);
        System.out.println("Final after 10 years at 5%: " + finalAmount);
    }
}

This example shows practical uses of mathematical constants. We calculate circle properties using Math.PI and model exponential growth using Math.E. These constants are essential for many scientific and engineering calculations.

Advanced Mathematical Operations

The Math class also provides more advanced operations like hyperbolic functions, angle conversions, and IEEE remainder. These methods are useful for specialized mathematical computations.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Hyperbolic functions
        double x = 1.0;
        System.out.println("sinh(1.0): " + Math.sinh(x));
        System.out.println("cosh(1.0): " + Math.cosh(x));
        System.out.println("tanh(1.0): " + Math.tanh(x));
        
        // IEEE remainder
        System.out.println("\nIEEE remainder:");
        System.out.println("IEEEremainder(10, 3): " + 
                          Math.IEEEremainder(10, 3));
        System.out.println("10 % 3: " + (10 % 3));
        
        // Copy sign
        System.out.println("\nCopy sign:");
        System.out.println("copySign(5.0, -1.0): " + 
                          Math.copySign(5.0, -1.0));
        System.out.println("copySign(-5.0, 1.0): " + 
                          Math.copySign(-5.0, 1.0));
    }
}

This example demonstrates advanced Math class operations. Hyperbolic functions are similar to trigonometric functions but based on hyperbolas. The IEEE remainder differs from the modulo operator in how it handles negative numbers. copySign is useful for manipulating number signs.

Source

Java Math Class Documentation

In this article, we've covered all major methods of the Java Math class with practical examples. These methods provide essential mathematical operations that are fundamental to many programming tasks.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.