Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Java Math


Java Math

In Java, the java.lang.Math class provides a set of methods for performing mathematical operations. These methods are static, which means you don't need to create an instance of Math to use them. Here are some commonly used methods from the Math class:

  1. Trigonometric Functions:

    • double Math.sin(double a): Returns the sine of the specified angle a in radians.
    • double Math.cos(double a): Returns the cosine of the specified angle a in radians.
    • double Math.tan(double a): Returns the tangent of the specified angle a in radians.
    • double Math.atan(double a): Returns the arctangent of the specified value a in radians.
  2. Exponential and Logarithmic Functions:

    • double Math.exp(double a): Returns the exponential value of a, where e is the base of the natural logarithm.
    • double Math.log(double a): Returns the natural logarithm (base e) of a.
    • double Math.pow(double base, double exponent): Returns the value of the first argument raised to the power of the second argument.
  3. Square Root and Exponentiation:

    • double Math.sqrt(double a): Returns the square root of the specified value a.
    • double Math.cbrt(double a): Returns the cube root of the specified value a.
    • double Math.pow(double base, double exponent): Returns the value of the first argument raised to the power of the second argument.
  4. Rounding Functions:

    • double Math.round(double a): Returns the closest long to the argument, with ties rounding to positive infinity.
    • double Math.floor(double a): Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
  5. Random Number Generation:

    • double Math.random(): Returns a pseudorandom double value between 0.0 and 1.0.

Here's an example demonstrating the usage of some of these methods:


    public class Main {
        public static void main(String[] args) {
            double angle = Math.PI / 4; // 45 degrees in radians
    
            // Trigonometric functions
            System.out.println("sin(45 degrees) = " + Math.sin(angle));
            System.out.println("cos(45 degrees) = " + Math.cos(angle));
            System.out.println("tan(45 degrees) = " + Math.tan(angle));
            System.out.println("atan(1) = " + Math.atan(1)); // arctan(1) = 45 degrees
    
            // Exponential and Logarithmic functions
            System.out.println("exp(1) = " + Math.exp(1)); // e^1
            System.out.println("log(e) = " + Math.log(Math.E)); // log(e) = 1
    
            // Square root and exponentiation
            System.out.println("sqrt(16) = " + Math.sqrt(16));
            System.out.println("cbrt(27) = " + Math.cbrt(27));
            System.out.println("2^3 = " + Math.pow(2, 3));
    
            // Rounding functions
            System.out.println("Round(3.7) = " + Math.round(3.7));
            System.out.println("Floor(3.7) = " + Math.floor(3.7));
    
            // Random number generation
            System.out.println("Random number between 0 and 1: " + Math.random());
        }
    }

Output:


sin(45 degrees) = 0.7071067811865475
cos(45 degrees) = 0.7071067811865476
tan(45 degrees) = 0.9999999999999999
atan(1) = 0.7853981633974483
exp(1) = 2.718281828459045
log(e) = 1.0
sqrt(16) = 4.0
cbrt(27) = 3.0
2^3 = 8.0
Round(3.7) = 4
Floor(3.7) = 3.0
Random number between 0 and 1: 0.8931348785486834

These are just a few examples of what the Math class offers. It's a powerful tool for performing various mathematical calculations in Java



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java