Explain Math Functions in Java with example

Math Functions in Java: Here are some common math functions in Java, along with examples of how to use them:

Math Functions in Java

Java provides a Math class in the java.lang package which contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

Absolute Value

int a = -10;
int absValue = Math.abs(a);  // absValue will be 10

Maximum and Minimum

int x = 5;
int y = 10;
int max = Math.max(x, y);  // max will be 10
int min = Math.min(x, y);  // min will be 5

Power

double base = 2;
double exponent = 3;
double result = Math.pow(base, exponent);  // result will be 8.0

Square Root

double number = 16;
double sqrtResult = Math.sqrt(number);  // sqrtResult will be 4.0

Rounding Functions

double num = 5.65;
long rounded = Math.round(num);  // rounded will be 6
double floor = Math.floor(num);  // floor will be 5.0
double ceil = Math.ceil(num);    // ceil will be 6.0

Trigonometric Functions in Java

Sine, Cosine, and Tangent

double angle = Math.toRadians(45);  // Convert angle to radians
double sinValue = Math.sin(angle);  // sinValue will be 0.7071067811865475
double cosValue = Math.cos(angle);  // cosValue will be 0.7071067811865476
double tanValue = Math.tan(angle);  // tanValue will be 0.9999999999999999

Exponential and Logarithmic Functions

Exponential

double expValue = Math.exp(1);  // expValue will be 2.718281828459045

Logarithm

double logValue = Math.log(10);  // logValue will be 2.302585092994046
double log10Value = Math.log10(10);  // log10Value will be 1.0

Random Number Generation

Random Number

double randomValue = Math.random();  // randomValue will be a value between 0.0 and 1.0

Examples of Using Math Functions in Java

Here is a complete example demonstrating the use of some math functions:

public class MathFunctionsExample {
    public static void main(String[] args) {
        // Absolute value
        int a = -10;
        System.out.println("Absolute value: " + Math.abs(a));

        // Maximum and Minimum
        int x = 5;
        int y = 10;
        System.out.println("Maximum: " + Math.max(x, y));
        System.out.println("Minimum: " + Math.min(x, y));

        // Power
        double base = 2;
        double exponent = 3;
        System.out.println("Power: " + Math.pow(base, exponent));

        // Square Root
        double number = 16;
        System.out.println("Square Root: " + Math.sqrt(number));

        // Rounding Functions
        double num = 5.65;
        System.out.println("Rounded: " + Math.round(num));
        System.out.println("Floor: " + Math.floor(num));
        System.out.println("Ceil: " + Math.ceil(num));

        // Trigonometric Functions
        double angle = Math.toRadians(45);  // Convert angle to radians
        System.out.println("Sine: " + Math.sin(angle));
        System.out.println("Cosine: " + Math.cos(angle));
        System.out.println("Tangent: " + Math.tan(angle));

        // Exponential and Logarithmic Functions
        System.out.println("Exponential: " + Math.exp(1));
        System.out.println("Logarithm (natural): " + Math.log(10));
        System.out.println("Logarithm (base 10): " + Math.log10(10));

        // Random Number
        System.out.println("Random Number: " + Math.random());
    }
}

This example demonstrates how to use various math functions provided by the Math class in Java.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *