Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Math Methods in Java

Java Math Methods in Java

Math Methods in Java

The Math class in Java provides a wide range of methods for performing various mathematical operations. These methods are static, meaning you can call them directly using the class name without creating an instance of Math. Below is a breakdown of the commonly used Math methods in Java:


1. Arithmetic Methods

  • abs(double a): Returns the absolute value of a.

    Math.abs(-5.5);  // Returns 5.5
  • max(double a, double b): Returns the maximum of two numbers.

    Math.max(10, 20);  // Returns 20
  • min(double a, double b): Returns the minimum of two numbers.

    Math.min(10, 20);  // Returns 10
  • pow(double a, double b): Returns a raised to the power of b.

    Math.pow(2, 3);  // Returns 8.0 (2^3)
  • sqrt(double a): Returns the square root of a.

    Math.sqrt(16);  // Returns 4.0
  • cbrt(double a): Returns the cube root of a.

    Math.cbrt(27);  // Returns 3.0
  • round(double a): Returns the closest long to a, rounding up if necessary.

    Math.round(3.5);  // Returns 4
  • random(): Returns a random number between 0.0 (inclusive) and 1.0 (exclusive).

    Math.random();  // Returns a random number between 0.0 and 1.0

2. Trigonometric Methods

  • sin(double a): Returns the sine of angle a (in radians).

    Math.sin(Math.PI / 2);  // Returns 1.0
  • cos(double a): Returns the cosine of angle a (in radians).

    Math.cos(Math.PI);  // Returns -1.0
  • tan(double a): Returns the tangent of angle a (in radians).

    Math.tan(Math.PI / 4);  // Returns 1.0
  • asin(double a): Returns the arcsine of a (in radians).

    Math.asin(1);  // Returns 1.5708 (?/2)
  • acos(double a): Returns the arccosine of a (in radians).

    Math.acos(1);  // Returns 0.0
  • atan(double a): Returns the arctangent of a (in radians).

    Math.atan(1);  // Returns 0.7854 (?/4)
  • toDegrees(double a): Converts an angle from radians to degrees.

    Math.toDegrees(Math.PI);  // Returns 180.0
  • toRadians(double a): Converts an angle from degrees to radians.

    Math.toRadians(180);  // Returns 3.1416 (?)

3. Logarithmic Methods

  • log(double a): Returns the natural logarithm (base e) of a.

    Math.log(10);  // Returns 2.302585
  • log10(double a): Returns the base 10 logarithm of a.

    Math.log10(100);  // Returns 2.0
  • exp(double a): Returns e raised to the power of a.

    Math.exp(2);  // Returns 7.389056
  • expm1(double a): Returns e^a - 1.

    Math.expm1(1);  // Returns 1.7182818

4. Hyperbolic Methods

  • sinh(double a): Returns the hyperbolic sine of a.

    Math.sinh(1);  // Returns 1.175201
  • cosh(double a): Returns the hyperbolic cosine of a.

    Math.cosh(1);  // Returns 1.5430806
  • tanh(double a): Returns the hyperbolic tangent of a.

    Math.tanh(1);  // Returns 0.761594

5. Miscellaneous Methods

  • PI: A constant for the value of Pi (?).

    Math.PI;  // Returns 3.141592653589793
  • E: A constant for the value of Euler's number (e).

    Math.E;  // Returns 2.718281828459045
  • ulp(double a): Returns the size of the smallest adjacent floating-point number to a.

    Math.ulp(2.0);  // Returns 4.44089209850063E-16

Example Program Using Math Methods

Here is a sample program that demonstrates the usage of various Math methods:

public class MathMethodsExample {    public static void main(String[] args) {        // Arithmetic Methods        System.out.println("Absolute value of -10.5: " + Math.abs(-10.5));  // 10.5        System.out.println("Max of 5 and 10: " + Math.max(5, 10));  // 10        System.out.println("2 raised to the power of 3: " + Math.pow(2, 3));  // 8.0        // Trigonometric Methods        System.out.println("Sine of 90 degrees: " + Math.sin(Math.toRadians(90)));  // 1.0        System.out.println("Cosine of 0 degrees: " + Math.cos(Math.toRadians(0)));  // 1.0        // Logarithmic Methods        System.out.println("Natural log of 10: " + Math.log(10));  // 2.302585        System.out.println("Base 10 log of 100: " + Math.log10(100));  // 2.0        // Random Method        System.out.println("Random number: " + Math.random());        // Hyperbolic Methods        System.out.println("Hyperbolic sine of 1: " + Math.sinh(1));  // 1.175201    }}

Sample Output:

Absolute value of -10.5: 10.5Max of 5 and 10: 102 raised to the power of 3: 8.0Sine of 90 degrees: 1.0Cosine of 0 degrees: 1.0Natural log of 10: 2.302585Base 10 log of 100: 2.0Random number: 0.2747473649370256Hyperbolic sine of 1: 1.175201

Conclusion

The Math class in Java offers a comprehensive set of static methods to perform a wide range of mathematical calculations, from basic arithmetic and trigonometry to logarithmic and hyperbolic operations. These methods are highly efficient and make it easy to implement mathematical logic in your programs.

Let me know if you need further clarification or examples! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql